curl 的用法指南

作者:阮一峯

www.ruanyifeng.com/blog/2019/09/curl-reference.html

簡介

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

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

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

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

1$ curl https://www.example.com
2
3

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

-A

-A 參數指定客戶端的用戶代理標頭,即 User-Agent。curl 的默認用戶代理字符串是 curl/[version]。

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://google.com
2
3

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

1$ curl -A '' https://google.com
2
3

上面命令會移除 User-Agent 標頭。

也可以通過 - H 參數直接指定標頭,更改 User-Agent。

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

-b

-b 參數用來向服務器發送 Cookie。

1$ curl -b 'foo=bar' https://google.com
2
3

上面命令會生成一個標頭 Cookie: foo=bar,向服務器發送一個名爲 foo、值爲 bar 的 Cookie。

1$ curl -b 'foo1=bar;foo2=bar2' https://google.com
2
3

上面命令發送兩個 Cookie。

1$ curl -b cookies.txt https://www.google.com
2
3

上面命令讀取本地文件 cookies.txt,裏面是服務器設置的 Cookie(參見 - c 參數),將其發送到服務器。

-c

-c 參數將服務器設置的 Cookie 寫入一個文件。

1$ curl -c cookies.txt https://www.google.com
2
3

上面命令將服務器的 HTTP 迴應所設置 Cookie 寫入文本文件 cookies.txt。

-d

-d 參數用於發送 POST 請求的數據體。

1$ curl -d'login=emma&password=123'-X POST https://google.com/login
2# 或者
3$ curl -d 'login=emma' -d 'password=123' -X POST  https://google.com/login
4
5

使用 - d 參數以後,HTTP 請求會自動加上標頭 Content-Type : application/x-www-form-urlencoded。並且會自動將請求轉爲 POST 方法,因此可以省略 - X POST。

-d 參數可以讀取本地文本文件的數據,向服務器發送。

1$ curl -d '@data.txt' https://google.com/login
2
3

上面命令讀取 data.txt 文件的內容,作爲數據體向服務器發送。

--data-urlencode

--data-urlencode 參數等同於 - d,發送 POST 請求的數據體,區別在於會自動將發送的數據進行 URL 編碼。

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

上面代碼中,發送的數據 hello world 之間有一個空格,需要進行 URL 編碼。

-e

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

1$ curl -e 'https://google.com?q=example' https://www.example.com
2
3

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

-H 參數可以通過直接添加標頭 Referer,達到同樣效果。

1curl -H 'Referer: https://google.com?q=example' https://www.example.com
2
3

-F

-F 參數用來向服務器上傳二進制文件。

1$ curl -F 'file=@photo.png' https://google.com/profile
2
3

上面命令會給 HTTP 請求加上標頭 Content-Type: multipart/form-data,然後將文件 photo.png 作爲 file 字段上傳。

-F 參數可以指定 MIME 類型。

1$ curl -F 'file=@photo.png;type=image/png' https://google.com/profile
2
3

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

-F 參數也可以指定文件名。

1$ curl -F 'file=@photo.png;filename=me.png' https://google.com/profile
2
3

上面命令中,原始文件名爲 photo.png,但是服務器接收到的文件名爲 me.png。

-G

-G 參數用來構造 URL 的查詢字符串。

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

上面命令會發出一個 GET 請求,實際請求的 URL 爲 https://google.com/search?q=kitties&count=20。如果省略 --G,會發出一個 POST 請求。

如果數據需要 URL 編碼,可以結合 --data--urlencode 參數。

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

-H

-H 參數添加 HTTP 請求的標頭。

1$ curl -H 'Accept-Language: en-US' https://google.com
2
3

上面命令添加 HTTP 標頭 Accept-Language: en-US。

1$ curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com
2
3

上面命令添加兩個 HTTP 標頭。

1$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login
2
3

上面命令添加 HTTP 請求的標頭是 Content-Type: application/json,然後用 - d 參數發送 JSON 數據。

-i

-i 參數打印出服務器迴應的 HTTP 標頭。

1$ curl -i https://www.example.com
2
3

上面命令收到服務器迴應後,先輸出服務器迴應的標頭,然後空一行,再輸出網頁的源碼。

-I

-I 參數向服務器發出 HEAD 請求,然會將服務器返回的 HTTP 標頭打印出來。

1$ curl -I https://www.example.com
2
3

上面命令輸出服務器對 HEAD 請求的迴應。

--head 參數等同於 - I。

1$ curl --head https://www.example.com
2
3

-k

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

1$ curl -k https://www.example.com
2
3

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

-L

-L 參數會讓 HTTP 請求跟隨服務器的重定向。curl 默認不跟隨重定向。

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

--limit-rate

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

1$ curl --limit-rate 200k https://google.com
2
3

上面命令將帶寬限制在每秒 200K 字節。

-o

-o 參數將服務器的迴應保存成文件,等同於 wget 命令。

1$ curl -o example.html https://www.example.com
2
3

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

-O

-O 參數將服務器迴應保存成文件,並將 URL 的最後部分當作文件名。

1$ curl -O https://www.example.com/foo/bar.html
2
3

上面命令將服務器迴應保存成文件,文件名爲 bar.html。

-s

-s 參數將不輸出錯誤和進度信息。

1$ curl -s https://www.example.com
2
3

上面命令一旦發生錯誤,不會顯示錯誤信息。不發生錯誤的話,會正常顯示運行結果。

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

1$ curl -s -o /dev/null https://google.com
2
3

-S

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

1$ curl -S -s -o /dev/null https://google.com
2
3

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

-u

-u 參數用來設置服務器認證的用戶名和密碼。

1$ curl -u 'bob:12345' https://google.com/login
2
3

上面命令設置用戶名爲 bob,密碼爲 12345,然後將其轉爲 HTTP 標頭 Authorization: Basic Ym9iOjEyMzQ1。

curl 能夠識別 URL 裏面的用戶名和密碼。

1$ curl https://bob:12345@google.com/login
2
3

上面命令能夠識別 URL 裏面的用戶名和密碼,將其轉爲上個例子裏面的 HTTP 標頭。

1$ curl -u 'bob' https://google.com/login
2
3

上面命令只設置了用戶名,執行後,curl 會提示用戶輸入密碼。

-v

-v 參數輸出通信的整個過程,用於調試。

1$ curl -v https://www.example.com
2
3

--trace 參數也可以用於調試,還會輸出原始的二進制數據。

1$ curl --trace - https://www.example.com
2
3

-x

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

1$ curl -x socks5://james:cats@myproxy.com:8080 https://www.example.com
2
3

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

如果沒有指定代理協議,默認爲 HTTP。

1$ curl -x james:cats@myproxy.com:8080 https://www.example.com
2
3

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

-X

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

1$ curl -X POST https://www.example.com
2
3

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

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/dz--dwJi0UUCCL4xnY8v4A