原文来自一位大佬的文章,此处仅仅作为学习笔记,原文地址:https://www.ruanyifeng.com/blog/2011/09/curl.html
简介
如果你了解Postman的话,那么curl就很好解释,你可以把curl理解为命令行版的Postman,没错curl的c实际就是Client(客户端),它就是一个发请求的命令
基本用法
curl会向你指定的URL发送GET请求并将得到的数据输出在标准输出中,例如:
输出类似下面这样:
1 2 3 4
| <!DOCTYPE html> <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomp
略......
|
可以看到这正是百度首页的HTML文档
-i参数
带上这个参数可以连同Response Header一起显示出来
输出如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| HTTP/1.1 200 OK Accept-Ranges: bytes Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform Connection: keep-alive Content-Length: 2381 Content-Type: text/html Date: Mon, 21 Oct 2024 11:22:09 GMT Etag: "588604c8-94d" Last-Modified: Mon, 23 Jan 2017 13:27:36 GMT Pragma: no-cache Server: bfe/1.0.8.18 Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
<!DOCTYPE html> <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.bai
|
-I参数
这个参数也是将Response Head显示出来,但是不会同时显示请求的数据内容
-o参数
这个参数可以让你将输出的内容保存到一个文件中:
1
| curl -o baidu.html www.baidu.com
|
发送其他类型的HTTP请求
curl使用 -X 参数来指定HTTP请求方法:
1 2
| curl -X POST [URL] curl -X DELETE [URL]
|
带参数和Data
参数直接写在URL中就可以了:
1
| curl -X POST [URL]?para1=value1¶2=value2
|
data可以使用--data
选项
1 2 3
| curl -X POST --data '{"username": "asd", "password":"123"}' [URL] curl -X POST --data 'username=asd&password=123' [URL] curl -X POST --data-urlencode 'username=小明&password=123' [URL]
|
从上面可以看出,在指定data时,可以直接写JSON数据,这将以Raw方式向服务器传输数据
以数据名=数据值
的格式的数据会被封装到FormData里面,有多个参数时用&
隔开
使用--data-urlencode
可以让curl帮你编码,比如有中文和空格时。
模拟表单
请看如下的表单:
1 2 3 4
| <form method="POST" enctype='multipart/form-data' action="upload.cgi"> <input type=file name=upload> <input type=submit name=press value="OK"> </form>
|
等效的curl形式为:
1
| curl -X POST --form upload=@localFileName --form press=OK [URL]
|
在指定文件路径时一定要加上@,相对路径和绝对路径都是可以的
使用User-Agent
1
| curl --user-agent "[Your UA]" [URL]
|
可以通过上述命令在请求头中加入特定的User-Agent
保存和使用cookie
保存来自服务器的cookies:
1
| curl -c cookies http://www.baidu.com
|
这会将服务端发送过来的cookies保存到一个名为cookies的文件中
使用cookies:
1
| curl -b cookies http://www.baidu.com
|
-b参数后面需要跟一个cookies文件
临时指定cookies:
1
| curl --cookie "username=gc" http://www.baidu.com
|
增加响应头信息
使用–header参数来增加响应头
1
| curl --header "Content-Type:application/json" http://example.com
|