curl命令
curl(CommandLine Uniform Resource Locator或CommandLine URL),curl命令是在命令行方式下工作,利用URL的語法進(jìn)行數(shù)據(jù)的傳輸或者文件的傳輸。是一個利用URL語法在命令行下工作的文件傳輸工具。curl設(shè)計為:在無用戶交互下完成工作。
curl支持的協(xié)議包括:DICT、FILE、FTP、FTPS、GOPHER、HTTP、HTTPS、IMAP、IMAPS、LDAP、LDAPS、POP3、POP3S、RTMP、RTSP、SCP、SFTP、SMTP、SMTPS、TELNET、TFTP。
curl用法
curl [options…] <url>
curl參數(shù)
參數(shù)類型 | 參數(shù) | 描述 |
---|---|---|
請求 | -H "name: value" --header "name: value" |
(HTTP)添加一個http header(http請求頭) |
-H "name:" --header "name:" |
(HTTP)移除一個http header(http請求頭) | |
-A "string" --user-agent "string" |
(HTTP)設(shè)置HTTP請求頭User-Agent參數(shù) 服務(wù)器通過User-Agent可以判斷客戶端使用的瀏覽器名稱和操作系統(tǒng)類型,偽造此參數(shù)能導(dǎo)致服務(wù)器做出錯誤判斷 |
|
-e --referer |
(HTTP)設(shè)置訪問時的來源頁面,告訴http服務(wù)從哪個頁面進(jìn)入到此頁面 例如:-e "aiezu.com"相當(dāng)于“-H "Referer: www.qq.com"”; |
|
-G --get |
(HTTP)使用HTTP GET方式 |
|
-d @file -d "string" --data "string" --data-ascii "string" --data-binary "string" --data-urlencode "string" |
(HTTP)使用HTTP POST方式發(fā)送“key/value對”數(shù)據(jù) 相當(dāng)于(method="POST",enctype="application/x-www-form-urlencoded") -d,--data:HTTP方式POST數(shù)據(jù) --data-ascii:HTTP方式POST ascii數(shù)據(jù) --data-binary:HTTP方式POST二進(jìn)制數(shù)據(jù) --data-urlencode:HTTP方式POST數(shù)據(jù)(進(jìn)行urlencode) 如果數(shù)據(jù)以“@”開頭,后緊跟一個文件,將post文件內(nèi)的內(nèi)容; |
|
-F name=@file -F name=<file -F name=content --form name=content --form-string <key=value> |
(HTTP)使用HTTP POST方式發(fā)送類似“表單字段”的多類型數(shù)據(jù) 相當(dāng)于同時設(shè)置瀏覽器表單屬性(method="POST",enctype="multipart/form-data"),可以使用此參數(shù)上傳二進(jìn)制文件。 如果字段內(nèi)容以“@”開頭,剩下的部分應(yīng)該是文件名,curl將會上傳此文件,如: curl -F "pic=@pic.jpg" http://aiezu.com; curl -F "page=@a.html;type=text/html" http://aiezu.com curl -F "page=@/tmp/a;filename=a.txt" http://aiezu.com 如果字段內(nèi)容以“<”開頭,剩下的部分應(yīng)該是文件名,curl將從文件中獲取作為此字段的值,如:curl -F "text=<text.txt" http://aiezu.com; --form-string類似于“--form”,但是“@”、“<”無特殊含義; |
|
-X <command> --request <command> |
(HTTP)指定與服務(wù)器通信使用的請求方法 如:GET、PUT、POST、DELETE等 |
|
cookie | -b name=data --cookie name=data |
(HTTP)發(fā)送cookie數(shù)據(jù)到HTTP服務(wù)器 數(shù)據(jù)格式為:"NAME1=VALUE1; NAME2=VALUE2" 如果行中沒有“=”,將把參數(shù)值當(dāng)作cookie文件名 這個cookie數(shù)據(jù)可以是由服務(wù)器的http響應(yīng)頭“Set-Cookie:”行發(fā)送過來的; |
-c filename --cookie-jar file name |
(HTTP)完成操作后將服務(wù)器返回的cookies保存到指定的文件 指定參數(shù)值為“-”將定向到標(biāo)準(zhǔn)輸出“如控制臺” |
|
-j --junk-session-cookies |
(HTTP)告訴curl放棄所有的"session cookies".相當(dāng)于重啟瀏覽器 | |
響應(yīng) | -I --head |
(HTTP)只輸出HTTP-header,不獲取內(nèi)容(HTTP/FTP/FILE) 1. 用于HTTP服務(wù)時,獲取頁面的http頭,如:curl -I http://aiezu.com 2. 用于FTP/FILE時,將會獲取文件大小、最后修改時間,如:curl -I file://test.txt |
-i --include |
(HTTP)輸出HTTP頭和返回內(nèi)容 | |
-D <file> --dump-header <file> |
(HTTP)轉(zhuǎn)儲http響應(yīng)頭到指定文件 |
curl舉例
GET請求
curl 'http://127.0.0.1:8888/test'
curl -G 'http://127.0.0.1:8888/test'
curl --get 'http://127.0.0.1:8888/test'
curl -X GET 'http://127.0.0.1:8888/test'
curl --request GET 'http://127.0.0.1:8888/test'
當(dāng)有參數(shù)時,請進(jìn)行URL編碼,否則會可能會出錯
curl -X GET 'http://127.0.0.1:8888/test2?date=2022-02-22 11:22:33&bo=1'
報錯:HTTP Status 505 – HTTP Version Not Supported(因?yàn)閰?shù)帶空格)
正確寫法(對空格URL編碼)
curl -X GET 'http://127.0.0.1:8888/test2?date=2022-02-22%2011:22:33&bo=1'
POST請求
curl -X POST 'http://127.0.0.1:8888/test2?date=2022-02-22%2011:22:33&bo=11'
curl -X POST 'http://127.0.0.1:8888/test2' -d 'date=2022-02-22 11:22:33&bo=11'
curl -X POST 'http://127.0.0.1:8888/test2' --data 'date=2022-02-22 11:22:33&bo=11'
curl -X POST 'http://127.0.0.1:8888/test2' --data-urlencode 'date=2022-02-22 11:22:33' --data-urlencode 'bo=11'
curl -X POST -H 'Content-Type: application/x-www-form-urlencoded' 'http://127.0.0.1:8888/test2' --data-urlencode 'date=2022-02-22 11:22:33' --data-urlencode 'bo=11'
POST請求默認(rèn)Content-Type: application/x-www-form-urlencoded
可以多次使用 -d、–data選項(xiàng)來指定多個傳輸參數(shù),也可以使用一次 -d、–data選項(xiàng)來指定多個參數(shù)。當(dāng) Content-Type 為 application/x-www-form-urlencoded 時,-X POST 不可缺省。
POST JSON請求
curl -X POST 'http://127.0.0.1:8888/test8' -H 'Content-Type: application/json' -d '{"code": 123456789,"name": 1646278385537}'
curl 'http://127.0.0.1:8888/test8' -H 'Content-Type: application/json' -d '{"code": 123456789,"name": 1646278385537}'
Content-Type:application/json 時,默認(rèn)以 POST 方式提交,此時 -X POST 可以缺省。
參考文章:
https://blog.csdn.net/piaoranyuji/article/details/108593757文章來源:http://www.zghlxwxcb.cn/news/detail-611638.html
https://zhuanlan.zhihu.com/p/71888942文章來源地址http://www.zghlxwxcb.cn/news/detail-611638.html
到了這里,關(guān)于使用curl執(zhí)行Http請求的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!