1. 程式人生 > 實用技巧 >curl工具的使用

curl工具的使用

curl是常用的命令列工具,用來請求Web伺服器。curl的就是client url的意思。

不帶任何引數時,curl發出的是GET請求

curl https://www.example.com

-A

-A引數指定客戶端的使用者代理標頭,即User-Agent。curl的預設使用者代理字串是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標頭 curl -A '' https://google.com # 通過-H直接指定標頭 curl -H 'User-Agent:python/3.6'

-b

-b引數用來向伺服器傳送Cookie

curl -b 'username=qq' https://qq.com

上述命令會生成一個標頭Cookie:username=qq,向伺服器傳送一個名為username,值為qq的Cookie

curl -b 'username=qq;qqzone=1' https://qq.com

上面命令傳送兩個Cookie

curl -b cookies.txt https://
qq.com

上面的命令讀取本地檔案cookies.txt,裡面是伺服器設定的Cookie,將其傳送到伺服器

-c

-c引數將伺服器設定的Cookie寫入一個檔案

curl -c cookies.txt https://www.google.com

上述命令將伺服器的HTTP響應所設定Cookie寫入本地文字檔案cookies.txt

-d

-d引數用於傳送POST請求的資料體

curl -d 'login=emmmm&password=23333' -X POST https://google.com/login
# 或者
curl -d 'login=emmmm' -d 'password=23333' -X POST https://
google.com/login

使用-d引數以後,HTTP請求會自動加上標頭

Content-Type:application/x-www-form-urlencoded

並且會自動將請求轉化為POST方法,因此可以省略-X POST

-d引數也可以讀取本地文字檔案的資料,向伺服器傳送。

curl -d 'data.txt' https://google.com/login

上面命令讀取data.txt檔案的內容,作為資料體向伺服器傳送

--data-urlencode

--data-urlencode引數等同於-d,傳送POST請求的資料體,區別在於會自動將傳送的資料進行URL編碼

curl --data-urlencode 'comment=hello world' https://google.com/login

上面程式碼中,傳送的資料hello world之間有一個空格,需要進行 URL 編碼

-F

-F引數用來向伺服器上傳二進位制檔案

curl -F '[email protected]' https://google.com/profile

上面命令給HTTP請求加上標頭Content-Type:multipart/form-data,然後將檔案photo.png作為file欄位上傳

-F引數可以指定MIME型別

curl -F '[email protected];type=image/png' https://google.com/profile

-F引數也可以指定檔名

curl -F '[email protected];filename=me.png' https://google.com/profile

上面命令中,原始檔名為photo.png,但是伺服器接收到的檔名為me.png

-G

-G引數用來構造URL的查詢字串

curl -G -d 'q=kitties' -d 'count=20' https://google.com/search

上述命令會發出一個GET請求,實際請求的URL為

https://google.com/search?q=kitties&count=20,注意如果忽略-G,會發出一個

POST請求

如果資料需要URL編碼。可以結合--data-urlencode引數

curl -G --data-urlencode 'comment=hello world' https://www.example.com

-i

-i引數打印出伺服器響應的HTTP標頭

curl -i https://www.example.com
上面命令收到伺服器響應後,先輸出伺服器響應頭,然後空一行,再網頁的原始碼輸出

-I

-I引數向伺服器發出HEAD請求,然後將伺服器返回的HTTP標頭打印出來

curl -I https://www.example.com

上面命令輸出對HEAD請求的響應

--head引數等同於-I

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

-k

引數指定跳過SSL檢測

curl -k https://www.example.com

-L

-L引數會讓HTTP請求跟隨伺服器的重定向。curl預設不跟隨重定向

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

--limit-rate

--limit-rate用來限制HTTP請求和響應的頻寬,模擬慢網速環境

curl --limit-rate 200k https://google.com

-o

-o引數將伺服器的響應儲存成檔案,等同於wget命令

curl -o example.html https://www.example.com

上面命令將www.example.com儲存成example.html

-O

-O引數將伺服器響應儲存成檔案,並將URL的最後部分當作檔名

curl -O https://www.example.com/foo/bar.html

上面命令將伺服器響應儲存成檔案,檔名bar.html

-S

-s引數將不輸出錯誤和進度資訊

curl -s https://www.example.com

上面命令一旦發生錯誤,不會顯示錯誤資訊。不發生錯誤的話,會正常顯示執行結果

如果想讓curl不產生任何輸出,可以使用下面的命令

curl -s -o /dev/null https://google.com

上面命令沒有任何輸出,除非發生錯誤

-v

-v引數輸出通訊的整個過程,用於除錯

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

-X

-X引數指定HTTP請求的方法

curl -X POST https://www.example.com

上面命令對https://www.example.com發出POST請求