( ^5 Q+ `$ q" M7 y+ D 简介curl 是常用的命令行工具,用来请求 Web 服务器它的名字就是客户端(client)的 URL 工具的意思它的功能非常强大,命令行参数多达几十种如果熟练的话,完全可以取代 Postman 这一类的图形界面工具。 9 S% }+ |- d1 J
本文介绍它的主要命令行参数,作为日常的参考,方便查阅内容主要翻译自《curl cookbook》为了节约篇幅,下面的例子不包括运行时的输出,初学者可以先看我以前写的《curl 初学者教程》不带有任何参数时,curl 就是发出 GET 请求。
" ^. ]. J+ `) k! S4 _: S $ curl https://www.example.com+ X! Y! s0 B7 Q' |( a) `. M4 l
上面命令向http://www.example.com发出 GET 请求,服务器返回的内容会在命令行输出-A-A参数指定客户端的用户代理标头,即User-Agent。
# d. d3 q9 m5 d) S% X4 r8 C curl 的默认用户代理字符串是curl/[version]$ curl -A Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36 https://google.com" v$ w" @3 n2 N# A# V8 `6 v
+ l& k" t- N1 O+ o 上面命令将User-Agent改成 Chrome 浏览器$ curl -A https://google.com
+ a* R: }7 W( s- d$ _" ` 上面命令会移除User-Agent标头也可以通过-H参数直接指定标头,更改User-Agent。
/ L" o* o6 B+ @- }$ d' Q# @0 d' A $ curl -H User-Agent: php/1.0 https://google.com
( M- G5 ~# P0 S4 y* [ -b-b参数用来向服务器发送 Cookie$ curl -b foo=bar https://google.com
6 Y) V8 Y3 z0 E, `: V" R 。
. {' i; V q& B' _; [ 上面命令会生成一个标头Cookie: foo=bar,向服务器发送一个名为foo、值为bar的 Cookie$ curl -b foo1=bar;foo2=bar2 https://google.com
7 h' b: b+ X2 ?3 n8 W9 Q. [- F 。 4 x! b% X; P5 D" j+ e( w' W
上面命令发送两个 Cookie$ curl -b cookies.txt https://www.google.com
6 Y+ b, z6 ~/ v( ? 上面命令读取本地文件cookies.txt,里面是服务器设置的 Cookie(参见-c参数),将其发送到服务器。 6 K" d, B) g9 B; g* w
-c-c参数将服务器设置的 Cookie 写入一个文件$ curl -c cookies.txt https://www.google.com
5 U; i; ^) r" G- [' n/ |$ O 上面命令将服务器的 HTTP 回应所设置 Cookie 写入文本文件cookies.txt。
/ O: y7 I. `7 F% g% x6 k- m -d-d参数用于发送 POST 请求的数据体$ curl -dlogin=emma&password=123-X POST https://google.com/login
. `$ y3 B9 H5 K# L # 或者
! |* i% H \7 x: J# V $ curl -d login=emma -d password=123 -X POST https://google.com/login& ~3 z! b8 G) ~7 s& Y+ ?
。 , _( H8 U, A5 A3 ?- ]' J# x9 }
使用-d参数以后,HTTP 请求会自动加上标头Content-Type : application/x-www-form-urlencoded并且会自动将请求转为 POST 方法,因此可以省略-X POST。
' o3 W1 f; g# m% y4 m: R/ G -d参数可以读取本地文本文件的数据,向服务器发送$ curl -d @data.txt https://google.com/login5 I+ r! P' a# Q0 H' K6 E
上面命令读取data.txt文件的内容,作为数据体向服务器发送--data-urlencode
/ A) A& x% h) O) w7 U M: c --data-urlencode参数等同于-d,发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码$ curl --data-urlencode comment=hello world https://google.com/login) M1 {7 _" a& c
。 9 l( l' q: b' J2 c' ~' T* P7 U7 S
上面代码中,发送的数据hello world之间有一个空格,需要进行 URL 编码-e-e参数用来设置 HTTP 的标头Referer,表示请求的来源$ curl -e https://google.com?q=example https://www.example.com y; P, K7 [: P3 A: H5 X
。
, H! O9 r" g/ H' g6 v' g 上面命令将Referer标头设为https://google.com?q=example-H参数可以通过直接添加标头Referer,达到同样效果curl -H Referer: https://google.com?q=example https://www.example.com
/ R- @* a1 X4 a' u( n, w1 o 。
% z4 m5 ~2 W- ]* N -F-F参数用来向服务器上传二进制文件$ curl -F file=@photo.png https://google.com/profile
: A: n: s3 z- ^9 ^8 k" I 上面命令会给 HTTP 请求加上标头Content-Type: multipart/form-data,然后将文件photo.png作为file字段上传。 ; J) o$ L# I: z
-F参数可以指定 MIME 类型$ curl -F file=@photo.png;type=image/png https://google.com/profile: o u7 f3 b- U! E; J, v5 i
上面命令指定 MIME 类型为image/png,否则 curl 会把 MIME 类型设为application/octet-stream。
4 {2 ?+ S4 c: V8 D+ B. p* T4 ]6 | -F参数也可以指定文件名$ curl -F file=@photo.png;filename=me.png https://google.com/profile, L$ I4 s1 w" X% s
上面命令中,原始文件名为photo.png,但是服务器接收到的文件名为me.png。
2 |0 ]& w- @" N1 @% ? -G-G参数用来构造 URL 的查询字符串$ curl -G -d q=kitties -d count=20 https://google.com/search
1 c. u. o' i+ T9 _ 上面命令会发出一个 GET 请求,实际请求的 URL 为。
- `& ]4 k6 b; @0 `9 y https://google.com/search?q=kitties&count=20如果省略--G,会发出一个 POST 请求如果数据需要 URL 编码,可以结合--data--urlencode参数。 + a/ C1 L2 @/ e4 _4 f! T+ p
$ curl -G --data-urlencode comment=hello world https://www.example.com
+ H/ N; Q" j; l" _: F3 V8 o- j -H-H参数添加 HTTP 请求的标头$ curl -H Accept-Language: en-US https://google.com( D7 P# u' n8 J$ I5 D7 k
。
m9 y* `- ]. Z 上面命令添加 HTTP 标头Accept-Language: en-US$ curl -H Accept-Language: en-US -H Secret-Message: xyzzy https://google.com, l% w4 M) J: Y7 `3 a' k. R- M
。
/ y p: I; Z! O0 P" }3 e 上面命令添加两个 HTTP 标头$ curl -d {"login": "emma", "pass": "123"} -H Content-Type: application/json https://google.com/login
$ a, x2 a' h7 S1 T2 d 。
) ?- h g) I/ {. k$ n, P 上面命令添加 HTTP 请求的标头是Content-Type: application/json,然后用-d参数发送 JSON 数据-i-i参数打印出服务器回应的 HTTP 标头$ curl -i https://www.example.com* X: c7 v' L) X0 f4 H. J) L
。 " d/ Y1 O5 _: A. c* v% C* C! a
上面命令收到服务器回应后,先输出服务器回应的标头,然后空一行,再输出网页的源码-I-I参数向服务器发出 HEAD 请求,然会将服务器返回的 HTTP 标头打印出来$ curl -I https://www.example.com6 p, T7 a+ ^$ H4 W+ [; D
。 # K9 s4 `) b4 C6 u" N6 @
上面命令输出服务器对 HEAD 请求的回应--head参数等同于-I$ curl --head https://www.example.com
5 M* ]- J1 R8 H1 n+ e3 u3 o$ _ -k-k参数指定跳过 SSL 检测$ curl -k https://www.example.com
5 f6 s: e1 H: }' S6 V D 。
" k( R% T' A% m 上面命令不会检查服务器的 SSL 证书是否正确-L-L参数会让 HTTP 请求跟随服务器的重定向curl 默认不跟随重定向$ curl -L -d tweet=hi https://api.twitter.com/tweet2 M3 A- k( ^: \5 }. H$ ~' K8 b
。
[' q. D) J }8 k --limit-rate--limit-rate用来限制 HTTP 请求和回应的带宽,模拟慢网速的环境$ curl --limit-rate 200k https://google.com
3 l: w9 h' y$ a/ d3 @1 j 上面命令将带宽限制在每秒 200K 字节。
# @2 N) E: t7 `+ X& I. i* i9 {4 l9 u -o-o参数将服务器的回应保存成文件,等同于wget命令$ curl -o example.html https://www.example.com) P6 s: g4 N9 d5 D
上面命令将http://www.example.com保存成example.html。 : |& M$ i) D5 E% n; O/ t. L9 b# m
-O-O参数将服务器回应保存成文件,并将 URL 的最后部分当作文件名$ curl -O https://www.example.com/foo/bar.html% q0 k3 g6 Z T% X0 A, e$ D
上面命令将服务器回应保存成文件,文件名为bar.html。
# u. p' s% q7 Q$ Q: f) M" O -s-s参数将不输出错误和进度信息$ curl -s https://www.example.com: C' t3 d- O# H6 A# I
上面命令一旦发生错误,不会显示错误信息不发生错误的话,会正常显示运行结果如果想让 curl 不产生任何输出,可以使用下面的命令。
O) L: R3 _3 b6 ? R $ curl -s -o /dev/null https://google.com8 L9 I- |7 Y Y3 c9 Y
-S-S参数指定只输出错误信息,通常与-s一起使用$ curl -S -s -o /dev/null https://google.com
5 q9 f* w9 x# x: S! y, m3 @ 。 1 A5 E# e) Z5 i# Z8 i- G
上面命令没有任何输出,除非发生错误-u-u参数用来设置服务器认证的用户名和密码$ curl -u bob:12345 https://google.com/login( z% o. D( i0 U; \
上面命令设置用户名为bob,密码为12345,然后将其转为 HTTP 标头Authorization: Basic Ym9iOjEyMzQ1。
1 Y6 Z6 Z& N/ A% [ curl 能够识别 URL 里面的用户名和密码$ curl https://bob:12345@google.com/login
: {) D# h0 _5 `2 h9 g( @) K9 H 上面命令能够识别 URL 里面的用户名和密码,将其转为上个例子里面的 HTTP 标头。
. P6 Q4 h* n8 I6 Y1 u $ curl -u bob https://google.com/login+ t! W3 M3 ~5 q: m0 w j' o) p
上面命令只设置了用户名,执行后,curl 会提示用户输入密码-v-v参数输出通信的整个过程,用于调试$ curl -v https://www.example.com" I" I! C& w/ _! h0 Z8 `0 m( l/ P. V
。
+ ? c% D8 @7 U/ U: B; k9 [: w --trace参数也可以用于调试,还会输出原始的二进制数据$ curl --trace - https://www.example.com
1 T% T- B: C6 _$ z) `& A" F -x-x参数指定 HTTP 请求的代理$ curl -x socks5://james:cats@myproxy.com:8080 https://www.example.com* j0 P3 I3 W1 N7 I6 A
。 ! _( x- {0 M2 Q9 J& i9 o
上面命令指定 HTTP 请求通过http://myproxy.com:8080的 socks5 代理发出如果没有指定代理协议,默认为 HTTP$ curl -x james:cats@myproxy.com:8080 https://www.example.com: V, g, f( P, B, @1 h6 ` [; U* m
。 4 _. _5 q) T9 c) i
上面命令中,请求的代理使用 HTTP 协议-X-X参数指定 HTTP 请求的方法$ curl -X POST https://www.example.com5 Z" r- O4 u. k2 O) |- T
上面命令对https://www.example.com发出 POST 请求。 - \; Y5 }0 b) T+ O
原文链接:http://www.ruanyifeng.com/blog/2019/09/curl-reference.html 作者: 阮一峰
' [: `: f3 v$ T3 a+ x& L7 E a8 l2 q) g9 r- T1 Q! B |
; D9 S$ P5 [/ O, Y
# [# k% f3 _" `4 N" r
7 e1 h( C( P6 U* @) d |