curl命令
curl是Linux下的一款常用的命令行工具,可以用来上传和下载 URL 指定的数据,请求 WEB 服务器等。
目前,curl 工具已经支持超过 200 多个选项,通过
1 | $ curl --help |
命令可以看到这些选项。如果想了解这些选项的详细信息,可以通过以下命令:
1 | $ curl -M |
一些常用选项与参数如下。
不带参数时,curl 就是发出 GET 请求:
1 2 3 4 | $ curl https://www.baidu.com <!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=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/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 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>'); </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html> |
其他的常用选项如下:
-A
1 | -A, --user-agent <name> |
指定客户端的用户代理标头,即User-Agent,curl默认的的用户代理字符串是 “curl/7.65.3”,其中7.65.3 是 curl 的版本,可以通过 curl -V 或 curl –version 查看。
通过 -A 选项可以指定不同的用户代理标头,例如将 User-Agent 改成是 Chrome 浏览器:
1 | $ 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://www.baidu.com |
或者移除标头:
1 | $ curl -A '' www.baidu.com |
-b
1 | -b, --cookie <data|filename> |
向服务器发送Cookie,可以直接指定表示 cookie 的字符串,也可以从指定的文件中读取出来发送给服务器。
1 2 | $ curl -b "a=b" www.baidu.com $ curl -b cookie.txt www.baidu.com |
-c
1 | -c, --cookie-jar <filename> |
将服务器设置的 cookie 写入指定文件,例如,将 www.baidu.com 服务器回应的 cookie 写入 cookie.txt 文件中:
1 | $ curl -c cookie.txt www.baidu.com |
-d
1 | -d, --data <data> |
发送 POST 请求的数据。
1 | $ curl -d "login=emma&password=123" https://www.baidu.com |
或者
1 | $ curl -d "login=emma" -d "password=123" https://www.baidu.com |
使用-d参数以后,HTTP 请求会自动加上标头 “Content-Type : application/x-www-form-urlencoded”,并且会自动将请求转为 POST 方法
另外,-d 也可以读取本地文本文件中的数据,向服务器发送,例如,创建data.txt 文件,内容为
1 | login=emma&password=123 |
之后执行以下命令:
1 | $ curl -d "@data.txt" https://www.baidu.com |
以上三个命令的执行效果是一样的,得到数据如下:
1 2 3 4 | => Send data, 23 bytes (0x17) 0000: 6c 6f 67 69 6e 3d 65 6d 6d 61 26 70 61 73 73 77 login=emma&passw 0010: 6f 72 64 3d 31 32 33 ord=123 == Info: upload completely sent off: 23 out of 23 bytes |
1 | --data-urlencode |
与 -d 选项的作用相同,区别是该选项会自动将发送的数据进行URL编码。
1 | $ curl --data-urlencode "login=emma&password=123" https://www.baidu.com |
跟踪数据如下,可以看到发送的数据是被编码的:
1 2 3 4 | => Send data, 27 bytes (0x1b) 0000: 6c 6f 67 69 6e 3d 65 6d 6d 61 25 32 36 70 61 73 login=emma%26pas 0010: 73 77 6f 72 64 25 33 44 31 32 33 sword%3D123 == Info: upload completely sent off: 27 out of 27 bytes |
-e
1 | -e, --referer <URL> |
设置 HTTP 的标头Referer,表示请求的来源。
1 | $ curl -e "www.sohu.com" www.baidu.com |
可以看到跟踪数据中的 referer:
1 2 3 4 5 6 7 8 9 | => Send header, 100 bytes (0x64) 0000: 47 45 54 20 2f 20 48 54 54 50 2f 31 2e 31 0d 0a GET / HTTP/1.1.. 0010: 48 6f 73 74 3a 20 77 77 77 2e 62 61 69 64 75 2e Host: www.baidu. 0020: 63 6f 6d 0d 0a 55 73 65 72 2d 41 67 65 6e 74 3a com..User-Agent: 0030: 20 63 75 72 6c 2f 37 2e 36 35 2e 33 0d 0a 41 63 curl/7.65.3..Ac 0040: 63 65 70 74 3a 20 2a 2f 2a 0d 0a 52 65 66 65 72 cept: */*..Refer 0050: 65 72 3a 20 77 77 77 2e 73 6f 68 75 2e 63 6f 6d er: www.sohu.com 0060: 0d 0a 0d 0a .... == Info: Mark bundle as not supporting multiuse |
-F
1 | -F, --form <name=content> |
向服务器上传二进制文件。
1 |
该命令会给 HTTP 请求加上标头Content-Type: multipart/form-data,然后将文件svg.png作为file字段上传。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | => Send header, 210 bytes (0xd2) 0000: 50 4f 53 54 20 2f 20 48 54 54 50 2f 31 2e 31 0d POST / HTTP/1.1. 0010: 0a 48 6f 73 74 3a 20 77 77 77 2e 62 61 69 64 75 .Host: www.baidu 0020: 2e 63 6f 6d 0d 0a 55 73 65 72 2d 41 67 65 6e 74 .com..User-Agent 0030: 3a 20 63 75 72 6c 2f 37 2e 36 35 2e 33 0d 0a 41 : curl/7.65.3..A 0040: 63 63 65 70 74 3a 20 2a 2f 2a 0d 0a 43 6f 6e 74 ccept: */*..Cont 0050: 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 34 35 39 34 ent-Length: 4594 0060: 35 37 0d 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65 57..Content-Type 0070: 3a 20 6d 75 6c 74 69 70 61 72 74 2f 66 6f 72 6d : multipart/form 0080: 2d 64 61 74 61 3b 20 62 6f 75 6e 64 61 72 79 3d -data; boundary= 0090: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------- 00a0: 2d 2d 2d 2d 2d 2d 2d 2d 30 64 31 63 30 65 64 37 --------0d1c0ed7 00b0: 66 62 34 61 61 35 66 66 0d 0a 45 78 70 65 63 74 fb4aa5ff..Expect 00c0: 3a 20 31 30 30 2d 63 6f 6e 74 69 6e 75 65 0d 0a : 100-continue.. 00d0: 0d 0a .. == Info: Mark bundle as not supporting multiuse <= Recv header, 23 bytes (0x17) 0000: 48 54 54 50 2f 31 2e 31 20 31 30 30 20 43 6f 6e HTTP/1.1 100 Con 0010: 74 69 6e 75 65 0d 0a tinue.. => Send data, 65536 bytes (0x10000) 0000: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------- 0010: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 30 64 31 63 30 65 ----------0d1c0e 0020: 64 37 66 62 34 61 61 35 66 66 0d 0a 43 6f 6e 74 d7fb4aa5ff..Cont 0030: 65 6e 74 2d 44 69 73 70 6f 73 69 74 69 6f 6e 3a ent-Disposition: 0040: 20 66 6f 72 6d 2d 64 61 74 61 3b 20 6e 61 6d 65 form-data; name 0050: 3d 22 66 69 6c 65 22 3b 20 66 69 6c 65 6e 61 6d ="file"; filenam 0060: 65 3d 22 73 76 67 2e 70 6e 67 22 0d 0a 43 6f 6e e="svg.png"..Con 0070: 74 65 6e 74 2d 54 79 70 65 3a 20 69 6d 61 67 65 tent-Type: image 0080: 2f 70 6e 67 0d 0a 0d 0a 89 50 4e 47 0d 0a 1a 0a /png.....PNG.... |
另外,-F 选项也可以指定 MIME 的类型,例如:
1 |
得到跟踪数据如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | => Send header, 210 bytes (0xd2) 0000: 50 4f 53 54 20 2f 20 48 54 54 50 2f 31 2e 31 0d POST / HTTP/1.1. 0010: 0a 48 6f 73 74 3a 20 77 77 77 2e 62 61 69 64 75 .Host: www.baidu 0020: 2e 63 6f 6d 0d 0a 55 73 65 72 2d 41 67 65 6e 74 .com..User-Agent 0030: 3a 20 63 75 72 6c 2f 37 2e 36 35 2e 33 0d 0a 41 : curl/7.65.3..A 0040: 63 63 65 70 74 3a 20 2a 2f 2a 0d 0a 43 6f 6e 74 ccept: */*..Cont 0050: 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 34 35 39 34 ent-Length: 4594 0060: 35 38 0d 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65 58..Content-Type 0070: 3a 20 6d 75 6c 74 69 70 61 72 74 2f 66 6f 72 6d : multipart/form 0080: 2d 64 61 74 61 3b 20 62 6f 75 6e 64 61 72 79 3d -data; boundary= 0090: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------- 00a0: 2d 2d 2d 2d 2d 2d 2d 2d 62 64 36 61 61 32 38 39 --------bd6aa289 00b0: 38 31 34 31 38 62 33 31 0d 0a 45 78 70 65 63 74 81418b31..Expect 00c0: 3a 20 31 30 30 2d 63 6f 6e 74 69 6e 75 65 0d 0a : 100-continue.. 00d0: 0d 0a .. == Info: Mark bundle as not supporting multiuse <= Recv header, 23 bytes (0x17) 0000: 48 54 54 50 2f 31 2e 31 20 31 30 30 20 43 6f 6e HTTP/1.1 100 Con 0010: 74 69 6e 75 65 0d 0a tinue.. => Send data, 65536 bytes (0x10000) 0000: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------- 0010: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 62 64 36 61 61 32 ----------bd6aa2 0020: 38 39 38 31 34 31 38 62 33 31 0d 0a 43 6f 6e 74 8981418b31..Cont 0030: 65 6e 74 2d 44 69 73 70 6f 73 69 74 69 6f 6e 3a ent-Disposition: 0040: 20 66 6f 72 6d 2d 64 61 74 61 3b 20 6e 61 6d 65 form-data; name 0050: 3d 22 66 69 6c 65 22 3b 20 66 69 6c 65 6e 61 6d ="file"; filenam 0060: 65 3d 22 73 76 67 2e 70 6e 67 22 0d 0a 43 6f 6e e="svg.png"..Con 0070: 74 65 6e 74 2d 54 79 70 65 3a 20 69 6d 61 67 65 tent-Type: image 0080: 2f 6a 70 65 67 0d 0a 0d 0a 89 50 4e 47 0d 0a 1a /jpeg.....PNG... |
更多不同的 MIME 类型可以参考网站:
https://www.iana.org/assignments/media-types/media-types.xhtml
另外,-F 选项也可以指定服务器端接收到的文件名:
1 |
即上传的原文件名为svg.png,但服务器接收到的文件名为 svg2.png(而上文的跟踪数据中可以看到,在没有特别指定时,服务器收到的文件名就是上传时候的文件名),跟踪数据如下:
1 2 3 4 5 6 7 8 9 10 | => Send data, 65536 bytes (0x10000) 0000: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------- 0010: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 66 61 30 38 38 35 ----------fa0885 0020: 35 62 38 32 33 61 65 66 32 66 0d 0a 43 6f 6e 74 5b823aef2f..Cont 0030: 65 6e 74 2d 44 69 73 70 6f 73 69 74 69 6f 6e 3a ent-Disposition: 0040: 20 66 6f 72 6d 2d 64 61 74 61 3b 20 6e 61 6d 65 form-data; name 0050: 3d 22 66 69 6c 65 22 3b 20 66 69 6c 65 6e 61 6d ="file"; filenam 0060: 65 3d 22 73 76 67 32 2e 70 6e 67 22 0d 0a 43 6f e="svg2.png"..Co 0070: 6e 74 65 6e 74 2d 54 79 70 65 3a 20 69 6d 61 67 ntent-Type: imag 0080: 65 2f 70 6e 67 0d 0a 0d 0a 89 50 4e 47 0d 0a 1a e/png.....PNG... |
-G
1 | -G, --get |
该选项用来构造 URL 的查询字符串,发出一个 GET 请求。如果省略该选项,则会发出一个 POST 请求。例如:
1 | $ curl -G -d "username=emma" -d "password=123" www.baidu.com/login |
该命令会发出一个 GET 请求,实际请求的 URL 为
1 | www.baidu.com/login?username=emma&password=123 |
跟踪数据如下:
1 2 3 4 5 6 7 8 | => Send header, 109 bytes (0x6d) 0000: 47 45 54 20 2f 6c 6f 67 69 6e 3f 75 73 65 72 6e GET /login?usern 0010: 61 6d 65 3d 65 6d 6d 61 26 70 61 73 73 77 6f 72 ame=emma&passwor 0020: 64 3d 31 32 33 20 48 54 54 50 2f 31 2e 31 0d 0a d=123 HTTP/1.1.. 0030: 48 6f 73 74 3a 20 77 77 77 2e 62 61 69 64 75 2e Host: www.baidu. 0040: 63 6f 6d 0d 0a 55 73 65 72 2d 41 67 65 6e 74 3a com..User-Agent: 0050: 20 63 75 72 6c 2f 37 2e 36 35 2e 33 0d 0a 41 63 curl/7.65.3..Ac 0060: 63 65 70 74 3a 20 2a 2f 2a 0d 0a 0d 0a cept: */*.... |
如果不加 -G 选项,则会发出一个 POST 请求,可参考前文。
-H
1 | -H, --header <header/@file> |
添加 HTTP 请求的标头,可以同时使用多个 -H 选项,添加多个标头:
1 | $ curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' www.baidu.com |
该命令添加两个标头,跟踪数据如下:
1 2 3 4 5 6 7 8 9 | => Send header, 124 bytes (0x7c) 0000: 47 45 54 20 2f 20 48 54 54 50 2f 31 2e 31 0d 0a GET / HTTP/1.1.. 0010: 48 6f 73 74 3a 20 77 77 77 2e 62 61 69 64 75 2e Host: www.baidu. 0020: 63 6f 6d 0d 0a 55 73 65 72 2d 41 67 65 6e 74 3a com..User-Agent: 0030: 20 63 75 72 6c 2f 37 2e 36 35 2e 33 0d 0a 41 63 curl/7.65.3..Ac 0040: 63 65 70 74 3a 20 2a 2f 2a 0d 0a 41 63 63 65 70 cept: */*..Accep 0050: 74 2d 4c 61 6e 67 75 61 67 65 3a 20 65 6e 2d 55 t-Language: en-U 0060: 53 0d 0a 53 65 63 72 65 74 2d 4d 65 73 73 61 67 S..Secret-Messag 0070: 65 3a 20 78 79 7a 7a 79 0d 0a 0d 0a e: xyzzy.... |
-i
1 | -i, --include |
该选项打印出服务器回应的 HTTP 标头。
1 | $ curl -i www.baidu.com |
该命令先打印出服务器回应的 HTTP 标头,再打印网页的源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $ curl -i www.baidu.com 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: Sun, 29 Mar 2020 12:56:31 GMT Etag: "588604c4-94d" Last-Modified: Mon, 23 Jan 2017 13:27:32 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.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 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html> |
-I
1 | -I, --head |
该选项向服务器发送 HEAD 请求,并显示服务器返回的标头。
1 2 3 4 5 6 7 8 9 10 11 12 | $ curl -I www.baidu.com HTTP/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: Sun, 29 Mar 2020 12:56:37 GMT Etag: "575e1f5c-115" Last-Modified: Mon, 13 Jun 2016 02:50:04 GMT Pragma: no-cache Server: bfe/1.0.8.18 |
-k
1 | -k, --insecure |
连接服务器时跳过 SSL 检查,可以通过非安全方式连接,不检查服务器的 SSL 证书是否正确。
1 | $ curl -k https://www.baidu.com |
-L
1 | -L, --location |
该选项会让 HTTP 请求跟随服务器的重定向(curl 默认不跟随重定向)。
1 | $ curl -L www.baidu.com/login |
使用 -L 选项和不使用 -L 选项,二者的输出不同。
–limit-rate
1 | --limit-rate <speed> |
限制 HTTP 请求和回应的带宽,模拟慢网速的环境。
1 | $ curl --limit-rate 10k www.baidu.com |
该命令将访问网页的带宽限制在10k字节。
-M
1 | -M, --manual |
显示 curl 的完整详细用法,该选项比 –help 显示的帮助信息更详细。
使用该选项相当于在命令行执行 man curl 命令。
-m
1 | -m, --max-time <seconds> |
允许传输的最大时间,单位是“秒”。
-n
1 | -n, --netrc |
必须读取 .netrc 文件中的用户名与密码。
1 | --netrc-file <filename> |
将指定的文件名代替 .netrc 文件。
-o
1 | -o, --output <file> |
该选项将服务器回应的内容保存成文件,等同于 wget 命令。
1 2 3 4 | $ curl -o baidu.html www.baidu.com % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 2381 100 2381 0 0 48591 0 --:--:-- --:--:-- --:--:-- 49604 |
-O
1 | -O, --remote-name |
该选项将服务器回应的内容保存成文件,并将 URL 的最后部分当作文件名,例如:
1 2 3 4 | $ curl -O http://mirrors.cn99.com/centos/8.1.1911/isos/x86_64/CentOS-8.1.1911-x86_64-dvd1.iso % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 1 7205M 1 74.8M 0 0 26.8M 0 0:04:28 0:00:02 0:04:26 26.8M |
该命令下载CentOS系统的镜像文件,并将指定 URL 的最后一部分作为下载后的镜像的文件名。
-r
1 | -r, --range <range> |
只接收服务器回应的
1 2 3 | $ curl -r 0-99 www.baidu.com --trace dump <!DOCTYPE html> <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charse |
该命令只接收了服务器返回的前 100 个字节。
-s
1 | -s, --silent |
静默模式,不输出错误和进度信息。
1 | $ curl -s -O http://mirrors.cn99.com/centos/8.1.1911/isos/x86_64/CentOS-8.1.1911-x86_64-dvd1.iso |
该命令会下载CentOS系统的镜像文件,但不会有任何输出。
-S
1 | -S, --show-error |
该选项要求输出错误信息,即使 -s 选项指定也会输出。例如:
1 2 | $ curl -S -s www.baidu.co curl: (6) Could not resolve host: www.baidu.co |
-T
1 | -T, --upload-file <file> |
将本地文件传输给服务器。
-u
1 | -u, --user <user:password> |
用来设置服务器认证的用户名与密码。
1 | $ curl -u "emma:123456" www.baidu.com |
该命令设置用户名为 emma,密码设置为 123456,然后将其转为 HTTP 标头 “Authorization: Basic ZW1tYToxMjM0NTY=”。跟踪数据如下:
1 2 3 4 5 6 7 8 9 10 11 | == Info: Connected to www.baidu.com (61.135.169.125) port 80 (#0) == Info: Server auth using Basic with user 'emma' => Send header, 121 bytes (0x79) 0000: 47 45 54 20 2f 6c 6f 67 69 6e 20 48 54 54 50 2f GET /login HTTP/ 0010: 31 2e 31 0d 0a 48 6f 73 74 3a 20 77 77 77 2e 62 1.1..Host: www.b 0020: 61 69 64 75 2e 63 6f 6d 0d 0a 41 75 74 68 6f 72 aidu.com..Author 0030: 69 7a 61 74 69 6f 6e 3a 20 42 61 73 69 63 20 5a ization: Basic Z 0040: 57 31 74 59 54 6f 78 4d 6a 4d 30 4e 54 59 3d 0d W1tYToxMjM0NTY=. 0050: 0a 55 73 65 72 2d 41 67 65 6e 74 3a 20 63 75 72 .User-Agent: cur 0060: 6c 2f 37 2e 36 35 2e 33 0d 0a 41 63 63 65 70 74 l/7.65.3..Accept 0070: 3a 20 2a 2f 2a 0d 0a 0d 0a : */*.... |
如果在命令中只指定用户名,则会在命令执行后,先提示用户输入密码,之后执行命令的过程与上文相同:
1 2 | $ curl -u "emma" www.baidu.com Enter host password for user 'emma': |
-v
1 | -v, --verbose |
该选项输出通信的整个过程,一般用于调试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | $ curl -v www.baidu.com * Trying 61.135.169.121:80... * TCP_NODELAY set * Connected to www.baidu.com (61.135.169.121) port 80 (#0) > GET / HTTP/1.1 > Host: www.baidu.com > User-Agent: curl/7.65.3 > Accept: */* > * Mark bundle as not supporting multiuse < 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: Sun, 29 Mar 2020 14:45:05 GMT < Etag: "588604c4-94d" < Last-Modified: Mon, 23 Jan 2017 13:27:32 GMT < Pragma: no-cache < Server: bfe/1.0.8.18 < Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/ < |
-x
1 | -x, --proxy [protocol://]host[:port] |
指定 HTTP 请求使用的代理,例如:
1 | $ curl -x socks5://emma:123456@myproxy.com:8080 www.baidu.com |
该命令指定 HTTP 请求通过 socks5 协议的代理 myproxy.com:8080 来访问页面。如果没有指定协议类型,则默认使用 HTTP 代理:
1 | $ curl -x emma:123456@myproxy.com:8080 www.baidu.com |
该命令使用 HTTP 协议的代理访问页面。
-X
1 | -X, --request <command> |
指定 HTTP 请求的方法,如GET、POST等,大部分情况下通常不需要使用该选项。
1 | $ curl -X POST www.baidu.com |
-Y
1 | -Y, --speed-limit <speed> |
当传输速度小于指定速度时,停止传输。
-y
1 | -y, --speed-time <seconds> |
在命令执行了指定时间之后,触发“speed-limit”。
参考:
- http://www.ruanyifeng.com/blog/2019/09/curl-reference.html
- http://www.ruanyifeng.com/blog/2011/09/curl.html
- 《curl 必知必会》
————————————————————