摘要:cURL的強大遠不止此,表單提交,上傳文件內容也不在話下,只需要使用 -F 或者 -D 參數, -F 會自動加上請求頭 Content-Type: multipart/form-data ,而 -D 則是 Content-Type : application/x-www-form-urlencoded .。$ curl -X POST --data "name=未讀代碼" http://wttr.in。

歡迎點擊關注 上方 “ 未讀代碼 ,文章每週更新

cURL Logo

cURL是一個開源免費項目,主要是命令行工具 cURL 和 libcurl, cURL 可以處理任何網絡傳輸協議,但是不涉及任何具體的 數據處理

cURL支持的通信協議非常豐富,如 DICT,FILE,FTP,FTPS,GOPHER,HTTP,HTTPS,IMAP,IMAPS,LDAP,LDAPS,MQTT,POP3,POP3S,RTMP, RTMPS,RTSP,SCP,SFTP,SMB,SMBS,SMTP,SMTPS,TELNET 以及 TFTP。查看 cURL 源代碼可以訪問官方 Github。

如果安裝 cURL 呢?

ubuntu / Debian.

sudo apt install curl

CentOS / Fedora.

sudo yum install curl

Windows.

如果你已經安裝了 Git,那麼 Git Bash 自帶 cURL . 如果作爲開發者你 git 都沒有,那麼只能官方手動下載。

1. 請求源碼

直接 curl 。

$ curl http://wttr.in/

上面請求的示例網址是一個天氣網站,很有意思,會根據你的請求 ip 信息返回你所在位置的天氣情況。

curl wttr.in

寫這篇文字時我所在的上海正在下雨,窗外飄雨無休無止。

2. 文件下載

使用 -o 保存文件,類似於 wget 命令,比如下載 README 文本保存爲 readme.txt 文件。如果你需要自定義文件名,可以使用 -O 自定使用 url 中的文件名。

$ curl -o readme.txt https://mirrors.nju.edu.cn/kali/README
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 159 100 159 0 0 1939 0 --:--:-- --:--:-- --:--:-- 1939

下載文件會顯示下載狀態,如數據量大小、傳輸速度、剩餘時間等。可以使用 -s 參數禁用進度表。

$ curl -o readme.txt https://mirrors.nju.edu.cn/kali/README
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 159 100 159 0 0 1939 0 --:--:-- --:--:-- --:--:-- 1939
$
$ curl -o readme.txt https://mirrors.nju.edu.cn/kali/README -s

也可以使用 --process-bar 參數讓進度表顯示爲進度條。

$ curl -o readme.txt https://mirrors.nju.edu.cn/kali/README --progress-bar
########################################################################################## 100.0%

cURL作爲強大的代名詞,斷點續傳自然手到擒來,使用 -C - 參數即可。下面是斷點續傳下載 ubuntu20.04 鏡像的例子。

$ curl -O https://mirrors.nju.edu.cn/ubuntu-releases/20.04/ubuntu-20.04-desktop-amd64.iso --progress-bar
## 1.7%
^C
$ curl -C - -O https://mirrors.nju.edu.cn/ubuntu-releases/20.04/ubuntu-20.04-desktop-amd64.iso --progress-bar
### 2.4%
^C
$ curl -C - -O https://mirrors.nju.edu.cn/ubuntu-releases/20.04/ubuntu-20.04-desktop-amd64.iso --progress-bar
### 2.7%
^C
$

什麼?下載時不想佔用太多網速?使用 --limit-rate 限個速吧。

curl -C - -O https://mirrors.nju.edu.cn/ubuntu-releases/20.04/ubuntu-20.04-desktop-amd64.iso --limit-rate 100k

什麼?你又要從 FTP 服務器下載文件了?不慌。

curl -u user:password -O ftp://ftp_server/path/to/file/

3.  Response Headers

使用 -i 參數顯示 Response Headers 信息。使用 -I 可以只顯示 Response Headers 信息。

$ curl -I http://wttr.in
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Sat, 30 May 2020 09:57:03 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 8678
Connection: keep-alive
Access-Control-Allow-Origin: *

4. 請求方式(GET/POST/...)

使用 -X 輕鬆更改請求方式。

$ curl -X GET http://wttr.in
$ curl -X POST http://wttr.in
$ curl -X PUT http://wttr.in
...

5. 請求參數

以傳入參數 name 值爲 未讀代碼 爲例。

Get 方式參數直接url拼接參數。

$ curl -X GET http://wttr.in?name=未讀代碼

Post 方式使用 --data 設置參數。

$ curl -X POST --data "name=未讀代碼" http://wttr.in

請求時也可以自定義 header 參數,使用 --harder 添加。

$ curl --header "Content-Type:application/json" http://wttr.in

6. 文件上傳

cURL的強大遠不止此,表單提交,上傳文件內容也不在話下,只需要使用 -F 或者 -D 參數, -F 會自動加上請求頭 Content-Type: multipart/form-data ,而 -D 則是 Content-Type : application/x-www-form-urlencoded .

比如上傳一個 protrait.jpg 圖片。

$ curl -F [email protected] https://example.com/upload

提交一個具有 name 和 age 參數的 form 表單。

curl -F name=Darcy -F age=18 https://example.com/upload

參數對應的內容也可以從文件中讀取。

curl -F "content=<達西的身世.txt" https://example.com/upload

上傳時同時指定內容類型。

curl -F "content=<達西的身世.txt;type=text/html" https://example.com/upload

上傳文件的和其他參數一起。

curl -F 'file=@"localfile";filename="nameinpost"' example.com/upload

7. 網址通配

cURL可以實現多個網址的匹配,你可以使用 {} 結合逗號分割來標識使用 url 中的某一段,也可以使用 [] 來表示範圍參數。

# 請求 www.baidu.com 和  pan.baidu.com 和 fanyi.baidu.com
$ curl http://{www,pan,fanyi}.baidu.com
# 虛構網址1-10開頭的baidu.com,然後請求
$ curl http://[1-10].baidu.com
# 虛構網址a-z開頭的baidu.com,然後請求
$ curl http://[a-z].baidu.com

這種方式有時候還是很有用處的,比如說你發現了某個網站的 url 規律。

8. 使用 cookie

請求時使用 -c 參數存儲響應的 cookie,使用 -b 可以在請求時帶上指定 cookie.

$ curl -c wdbyte_cookies http://www.wdbyte.com
$ curl -b wdbyte_cookes http://www.wdbyte.com

總結

以上就是 cURL 的常見用法了,最後告訴你一個小技巧,Chrome、Firefox 等瀏覽器可以直接拷貝請求爲 cURL 語句。保存之後下次請求測試非常方便。

Chrome 複製 cURL 請求

參考資料

  1. https://curl.haxx.se/docs/manpage.html

最後的話

文章有幫助可以點個「 在看 」或 分享 ,都是支持,我都喜歡!

文章每週持續更新, 迎關注「  未讀代碼  」公衆號 ,希望我們一起變得優秀。

一個「在看」,一段時光 :point_down:

相關文章