curl 的用法指南

參考鏈接

Curl Cookbook
Curl 的用法指南

簡介:curl 是常用的命令行工具,用來請求 Web 服務(wù)器。它的名字就是客戶端(client)的 URL 工具的意思。它的功能非常強(qiáng)大,命令行參數(shù)多達(dá)幾十種。如果熟練的話,完全可以取代 Postman 這一類的圖形界面工具。

本文介紹它的主要命令行參數(shù),作為日常的參考,方便查閱。內(nèi)容主要翻譯自 Curl 的用法指南

不帶有任何參數(shù)時,curl 就是發(fā)出 GET 請求

// 向www.example.com發(fā)出 GET 請求,服務(wù)器返回的內(nèi)容會在命令行輸出。
 $ curl https://www.example.com

-A

-A 參數(shù)指定客戶端的用戶代理標(biāo)頭,即User-Agent。curl 的默認(rèn)用戶代理字符串是curl/[version]。

// 將User-Agent改成 Chrome 瀏覽器。
$ 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
// 下面命令會移除User-Agent標(biāo)頭。
$ curl -A '' https://google.com

也可以通過-H參數(shù)直接指定標(biāo)頭,更改User-Agent。

$ curl -H 'User-Agent: php/1.0' https://google.com

-b

-b 參數(shù)用來向服務(wù)器發(fā)送 Cookie。

// 生成一個標(biāo)頭Cookie: foo=bar,向服務(wù)器發(fā)送一個名為foo、值為bar的 Cookie。
$ curl -b 'foo=bar' https://google.com
// 發(fā)送兩個 Cookie。
$ curl -b 'foo1=bar;foo2=bar2' https://google.com
// 讀取本地文件cookies.txt,里面是服務(wù)器設(shè)置的 Cookie(參見-c參數(shù)),將其發(fā)送到服務(wù)器。
$ curl -b cookies.txt https://www.google.com

-c

-c 參數(shù)將服務(wù)器設(shè)置的Cookie寫入一個文件。

// 將服務(wù)器的 HTTP 回應(yīng)所設(shè)置 Cookie 寫入文本文件cookies.txt。
$ curl -c cookies.txt https://www.google.com

-d

-d 參數(shù)用于發(fā)送 POST 請求的數(shù)據(jù)體。

$ curl -X POST -d'login=emma&password=123'-X POST https://google.com/login
// 或者
$ curl -d 'login=emma' -d 'password=123' -X POST  https://google.com/login
// 使用-d參數(shù)以后,HTTP 請求會自動加上標(biāo)頭Content-Type : application/x-www-form-urlencoded。并且會自動將請求轉(zhuǎn)為 POST 方法,因此可以省略-X POST。

-d 參數(shù)可以讀取本地文本文件的數(shù)據(jù),向服務(wù)器發(fā)送。

//讀取data.txt文件的內(nèi)容,作為數(shù)據(jù)體向服務(wù)器發(fā)送。
$ curl -d '@data.txt' https://google.com/login

--data-urlencode

--data-urlencode 參數(shù)等同于-d,發(fā)送 POST 請求的數(shù)據(jù)體,區(qū)別在于會自動將發(fā)送的數(shù)據(jù)進(jìn)行 URL 編碼。

// 發(fā)送的數(shù)據(jù)hello world之間有一個空格,需要進(jìn)行 URL 編碼。
$ curl --data-urlencode 'comment=hello world' https://google.com/login

-e

-e 參數(shù)用來設(shè)置 HTTP 的標(biāo)頭Referer,表示請求的來源。

// 將Referer標(biāo)頭設(shè)為https://google.com?q=example。
curl -e 'https://google.com?q=example' https://www.example.com
// -H參數(shù)可以通過直接添加標(biāo)頭Referer,達(dá)到同樣效果。
curl -H 'Referer: https://google.com?q=example' https://www.example.com

-F

-F 參數(shù)用來向服務(wù)器上傳二進(jìn)制文件。

// 給 HTTP 請求加上標(biāo)頭Content-Type: multipart/form-data ,然后將文件photo.png作為file字段上傳。
$ curl -F 'file=@photo.png' https://google.com/profile

-F 參數(shù)可以指定 MIME 類型。

// 指定 MIME 類型為image/png,否則 curl 會把 MIME 類型設(shè)為application/octet-stream。
$ curl -F 'file=@photo.png;type=image/png' https://google.com/profile

-F 參數(shù)也可以指定文件名。

// 原始文件名為photo.png,但是服務(wù)器接收到的文件名為me.png。
$ curl -F 'file=@photo.png;filename=me.png' https://google.com/profile

-G

-G 參數(shù)用來構(gòu)造 URL 的查詢字符串。

// 發(fā)出一個 GET 請求,實(shí)際請求的 URL 為https://google.com/search?q=kitties&count=20。如果省略-G,會發(fā)出一個 POST 請求。
$ curl -G -d 'q=kitties' -d 'count=20' https://google.com/search
// 如果數(shù)據(jù)需要 URL 編碼,可以結(jié)合--data--urlencode參數(shù)。
$ curl -G --data-urlencode 'comment=hello world' https://www.example.com

-H

-H 參數(shù)添加 HTTP 請求的標(biāo)頭。

// 添加 HTTP 標(biāo)頭Accept-Language: en-US。
$ curl -H 'Accept-Language: en-US' https://google.com
// 添加兩個 HTTP 標(biāo)頭。
$ curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com
// 添加 HTTP 請求的標(biāo)頭是Content-Type: application/json,然后用-d參數(shù)發(fā)送 JSON 數(shù)據(jù)
$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login

-i

-i 參數(shù)打印出服務(wù)器回應(yīng)的 HTTP 標(biāo)頭。

// 收到服務(wù)器回應(yīng)后,先輸出服務(wù)器回應(yīng)的標(biāo)頭,然后空一行,再輸出網(wǎng)頁的源碼。
$ curl -i https://www.example.com

-I (大寫i)

-I 參數(shù)向服務(wù)器發(fā)出 HEAD 請求,然會將服務(wù)器返回的 HTTP 標(biāo)頭打印出來。

// 輸出服務(wù)器對 HEAD 請求的回應(yīng)。
$ curl -I https://www.example.com

--head 參數(shù)等同于-I。

$ curl --head https://www.example.com

-k

-k 參數(shù)指定跳過 SSL 檢測。

// 不會檢查服務(wù)器的 SSL 證書是否正確。
$ curl -k https://www.example.com

-L

-L 參數(shù)會讓 HTTP 請求跟隨服務(wù)器的重定向。curl 默認(rèn)不跟隨重定向。

$ curl -L -d 'tweet=hi' https://api.twitter.com/tweet

--limit-rate

--limit-rate 用來限制 HTTP 請求和回應(yīng)的帶寬,模擬慢網(wǎng)速的環(huán)境

// 將帶寬限制在每秒 200K 字節(jié)。
$ curl --limit-rate 200k https://google.com

-o

-o 參數(shù)將服務(wù)器的回應(yīng)保存成文件,等同于wget命令。

// 上面命令將www.example.com保存成example.html。
$ curl -o example.html https://www.example.com

-O

-O 參數(shù)將服務(wù)器回應(yīng)保存成文件,并將 URL 的最后部分當(dāng)作文件名

// 將服務(wù)器回應(yīng)保存成文件,文件名為bar.html
$ curl -O https://www.example.com/foo/bar.html

-s

-s 參數(shù)將不輸出錯誤和進(jìn)度信息。

// 一旦發(fā)生錯誤,不會顯示錯誤信息。不發(fā)生錯誤的話,會正常顯示運(yùn)行結(jié)果。
$ curl -s https://www.example.com
// 如果想讓 curl 不產(chǎn)生任何輸出,可以使用下面的命令。
$ curl -s -o /dev/null https://google.com

-S

-S 參數(shù)指定只輸出錯誤信息,通常與-s 一起使用。

// 沒有任何輸出,除非發(fā)生錯誤。
$ curl -s -o /dev/null https://google.com

-u

-u 參數(shù)用來設(shè)置服務(wù)器認(rèn)證的用戶名和密碼。

// 設(shè)置用戶名為bob,密碼為12345,然后將其轉(zhuǎn)為 HTTP 標(biāo)頭Authorization: Basic Ym9iOjEyMzQ1
// curl 能夠識別 URL 里面的用戶名和密碼。
$ curl -u 'bob:12345' https://google.com/login
// 識別 URL 里面的用戶名和密碼,將其轉(zhuǎn)為上個例子里面的 HTTP 標(biāo)頭
$ curl https://bob:12345@google.com/login
// 只設(shè)置了用戶名,執(zhí)行后,curl 會提示用戶輸入密碼。
$ curl -u 'bob' https://google.com/login

-v

-v 參數(shù)輸出通信的整個過程,用于調(diào)試

$ curl -v https://www.example.com

--trace 參數(shù)也可以用于調(diào)試,還會輸出原始的二進(jìn)制數(shù)據(jù)

$ curl --trace - https://www.example.com

-x

-x 參數(shù)指定 HTTP 請求的代理

// 指定 HTTP 請求通過myproxy.com:8080的 socks5 代理發(fā)出
// 如果沒有指定代理協(xié)議,默認(rèn)為 HTTP
$ curl -x socks5://james:cats@myproxy.com:8080 https://www.example.com
// 請求的代理使用 HTTP 協(xié)議。
$ curl -x james:cats@myproxy.com:8080 https://www.example.com

-X

-X 參數(shù)指定 HTTP 請求的方法。

// 對https://www.example.com發(fā)出 POST 請求。
$ curl -X POST https://www.example.com
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容