可能是東半球最好的 Curl 學習指南,強烈建議收藏!

良許linux發表於2019-09-19

file

本文首發於:微信公眾號「運維之美」,公眾號 ID:Hi-Linux。

「運維之美」是一個有情懷、有態度,專注於 Linux 運維相關技術文章分享的公眾號。公眾號致力於為廣大運維工作者分享各類技術文章和釋出最前沿的科技資訊。公眾號的核心理念是:分享,我們認為只有分享才能使我們的團體更強大。如果你想第一時間獲取最新技術文章,歡迎關注我們!

公眾號作者 Mike,一個月薪 3000 的雜工。從事 IT 相關工作 15+ 年,熱衷於網際網路技術領域,認同開源文化,對運維相關技術有自己獨特的見解。很願意將自己積累的經驗、心得、技能與大家分享交流,篇篇乾貨不要錯過喲。如果你想聯絡到我,可關注公眾號獲取相關資訊。

簡介

curl 是常用的命令列工具,用來請求 Web 伺服器。它的名字就是客戶端(client)的 URL 工具的意思。

它的功能非常強大,命令列引數多達幾十種。如果熟練的話,完全可以取代 Postman 這一類的圖形介面工具。

使用例項

本文介紹它的主要命令列引數,作為日常的參考,方便查閱。內容主要翻譯自 《curl cookbook》。為了節約篇幅,下面的例子不包括執行時的輸出,初學者可以先看我以前寫的 《curl 初學者教程》。

不帶有任何引數時,curl 就是發出 GET 請求。

$ curl https://www.example.com複製程式碼

上面命令向 www.example.com 發出 GET 請求,伺服器返回的內容會在命令列輸出。

-A

-A 引數指定客戶端的使用者代理標頭,即 User-Agentcurl 的預設使用者代理字串是 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複製程式碼

上面命令將 User-Agent 改成 Chrome 瀏覽器。

$ curl -A '' https://google.com複製程式碼

上面命令會移除 User-Agent 標頭。你也可以通過 -H 引數直接指定標頭,更改 User-Agent

$ curl -H 'User-Agent: php/1.0' https://google.com複製程式碼

-b

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

$ curl -b 'foo=bar' https://google.com複製程式碼

上面命令會生成一個標頭 Cookie: foo=bar,向伺服器傳送一個名為 foo、值為 barCookie

$ curl -b 'foo1=bar' -b 'foo2=baz' https://google.com複製程式碼

上面命令傳送兩個 Cookie

$ curl -b cookies.txt https://www.google.com複製程式碼

上面命令讀取本地檔案 cookies.txt,裡面是伺服器設定的 Cookie(參見 -c 引數),將其傳送到伺服器。

-c

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

$ curl -c cookies.txt https://www.google.com複製程式碼

上面命令將伺服器的 HTTP 回應所設定 Cookie 寫入文字檔案 cookies.txt

-d

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

$ curl -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 引數以後,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 編碼。

-e

-e 引數用來設定 HTTP 的標頭 Referer,表示請求的來源。

$ curl -e 'https://google.com?q=example' https://www.example.com複製程式碼

上面命令將 Referer 標頭設為 https://google.com?q=example

-H 引數可以通過直接新增標頭 Referer,達到同樣效果。

$ curl -H 'Referer: https://google.com?q=example' https://www.example.com複製程式碼

-F

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

$ curl -F 'file=@photo.png' https://google.com/profile複製程式碼

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

-F 引數可以指定 MIME 型別。

$ curl -F 'file=@photo.png;type=image/png' https://google.com/profile複製程式碼

上面命令指定 MIME 型別為 image/png,否則 curl 會把 MIME 型別設為 application/octet-stream

-F 引數也可以指定檔名。

$ curl -F 'file=@photo.png;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複製程式碼

-H

-H 引數新增 HTTP 請求的標頭。

$ curl -H 'Accept-Language: en-US' https://google.com複製程式碼

上面命令新增 HTTP 標頭 Accept-Language: en-US

$ curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com複製程式碼

上面命令新增兩個 HTTP 標頭。

$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login複製程式碼

上面命令新增 HTTP 請求的標頭是 Content-Type: application/json,然後用 -d 引數傳送 JSON 資料。

-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

-k 引數指定跳過 SSL 檢測。

$ curl -k https://www.example.com複製程式碼

上面命令不會檢查伺服器的 SSL 證照是否正確。

-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複製程式碼

上面命令將頻寬限制在每秒 200K 位元組。

-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複製程式碼

-S

-S 引數指定只輸出錯誤資訊,通常與 -s 一起使用。

$ curl -s -o /dev/null https://google.com複製程式碼

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

-u

-u 引數用來設定伺服器認證的使用者名稱和密碼。

$ curl -u 'bob:12345' https://google.com/login複製程式碼

上面命令設定使用者名稱為 bob,密碼為 12345,然後將其轉為 HTTP 標頭 Authorization: Basic Ym9iOjEyMzQ1

curl 能夠識別 URL 裡面的使用者名稱和密碼。

$ curl https://bob:12345@google.com/login複製程式碼

上面命令能夠識別 URL 裡面的使用者名稱和密碼,將其轉為上個例子裡面的 HTTP 標頭。

$ curl -u 'bob' https://google.com/login複製程式碼

上面命令只設定了使用者名稱,執行後,curl 會提示使用者輸入密碼。

-v

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

$ curl -v https://www.example.com複製程式碼

--trace 引數也可以用於除錯,還會輸出原始的二進位制資料。

$ curl --trace - https://www.example.com複製程式碼

-x

-x 引數指定 HTTP 請求的代理。

$ curl -x socks5://james:cats@myproxy.com:8080 https://www.example.com複製程式碼

上面命令指定 HTTP 請求通過 myproxy.com:8080socks5 代理髮出。

如果沒有指定代理協議,預設為 HTTP

$ curl -x james:cats@myproxy.com:8080 https://www.example.com複製程式碼

上面命令中,請求的代理使用 HTTP 協議。

-X

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

$ curl -X POST https://www.example.com複製程式碼

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

參考連結

  • Curl Cookbook

來源:阮一峰的網路日誌

原文:http://t.cn/AiRQUQlz

題圖:來自谷歌圖片搜尋

版權:本文版權歸原作者所有

投稿:歡迎投稿,郵箱: editor@hi-linux.com

相關文章