curl 命令行工具
@(linux)[curl 帮助]
curl
是一种命令行工具,作用是发出网络请求,并获取数据,显示在标准输出stdout
上面,支持多种协议Usage: curl [options...] <url>
-i
&-I
-i
:显示http response 头部信息,连同网页代码一起-I
:只会显示http response 头部信息
$ curl -i http://www.sina.com
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 178 100 178 0 0 1412 0 --:--:-- --:--:-- --:--:-- 1447HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 15 Dec 2020 13:54:51 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: http://www.sina.com.cn/
Expires: Tue, 15 Dec 2020 13:56:31 GMT
Cache-Control: max-age=120
X-Via-SSL: ssl.95.sinag1.qxg.lb.sinanode.com
Edge-Copy-Time: 1608040489086
Age: 20
Via: https/1.1 cmcc.guangzhou.union.82 (ApacheTrafficServer/6.2.1 [cRs f ]), https/1.1 cmcc.shandong.union.25 (ApacheTrafficServer/6.2.1 [cRs f ])
X-Via-Edge: 16080404919570726f4786253c07856551a99
X-Cache: HIT.25
X-Via-CDN: f=edge,s=cmcc.shandong.union.26.nb.sinaedge.com,c=120.244.38.7;f=Edge,s=cmcc.shandong.union.25,c=120.192.83.26
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
$ curl -I http://www.sina.com
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 178 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 15 Dec 2020 14:00:32 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: http://www.sina.com.cn/
Expires: Tue, 15 Dec 2020 14:00:34 GMT
Cache-Control: max-age=120
X-Via-SSL: ssl.23.sinag1.qxg.lb.sinanode.com
Edge-Copy-Time: 1608040831144
Age: 118
Via: https/1.1 cmcc.guangzhou.union.82 (ApacheTrafficServer/6.2.1 [cRs f ]), https/1.1 cmcc.shandong.union.25 (ApacheTrafficServer/6.2.1 [cRs f ])
X-Via-Edge: 16080408327980726f4786253c0784f401845
X-Cache: HIT.25
X-Via-CDN: f=edge,s=cmcc.shandong.union.85.nb.sinaedge.com,c=120.244.38.7;f=Edge,s=cmcc.shandong.union.25,c=120.192.83.85
-k
-k
:在发送https请求时,不做认证(跳过 SSL 检测)- 不会检查服务器的 SSL 证书是否正确
- (SSL)允许curl执行不安全的SSL连接和传输。所有SSL连接都尝试使用默认安装的CA证书包来确保安全。
如果不采用-k 请求https时会出现以下提示:
Unable to communicate securely with peer: requested domain name does not match the server's certificate.
-H
-H param
:设置请求头参数-H "Content-type: application/json"
:设置请求头Content-Type,以json格式发送
$ curl -I -k -H "Content-type: application/json" http://www.baidu.com
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 277 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Tue, 15 Dec 2020 14:40:42 GMT
Etag: "575e1f60-115"
Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
-X
-X
:指定请求方法-X POST/PUT/DELETE
:使用POST/PUT/DELETE方法
$ curl -I -k -H "Content-type: application/json" -X GET http://www.baidu.com
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 2381 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0HTTP/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: Tue, 15 Dec 2020 14:43:40 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=/
-d
-d
:HTTP POST方式传送的数据windows
下使用curl
,-d
后面带有参数必须要使用双引号
,json格式的数据中双引号要用\
转义。Linux
下使用curl
,-d
后面的参数可以使用单引号(也可以双引号)
,json格式的数据使用双引号即可,将所有的变量转移成普通字符。