curl和jq的用法


簡介

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

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

1、curl可以看作命令行瀏覽器,不帶有任何參數時,curl 就是發出 GET 請求,查看網頁源代碼

curl https://www.example.com

2、開啟gzip請求

curl -I http://www.sina.com.cn/ -H Accept-Encoding:gzip,defalte

3、監控網頁的響應時間

$ curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" "http://www.sina.com"
time_connect: 0.039432
time_starttransfer: 0.045964
time_total: 0.051393

4、 監控站點可用性

$ curl -o /dev/null -s -w %{http_code} "http://www.sina.com"
200%

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

-A

-A參數指定客戶端的用戶代理標頭,即User-Agent。curl 的默認用戶代理字符串是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 瀏覽器。

也可以通過-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、值為bar的 Cookie。

$ curl -b 'foo1=bar;foo2=bar2' 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 標頭,查詢web服務器

curl -i www.sina.com
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 19 Feb 2021 01:56:19 GMT
Content-Type: text/html
Content-Length: 22981
Connection: keep-alive
Vary: Accept-Encoding
ETag: W/"5fed8659-831"V=5965C31
X-Powered-By: shci_v1.13
Expires: Fri, 19 Feb 2021 01:58:04 GMT
Cache-Control: max-age=120
X-Via-SSL: ssl.97.sinag1.bx.lb.sinanode.com
Edge-Copy-Time: 1613699764777
Age: 15
Via: https/1.1 cnc.yizhuang.union.95 (ApacheTrafficServer/6.2.1 [cRs f ])
X-Cache: HIT.unknown
X-Via-CDN: f=edge,s=cnc.yizhuang.union.94.nb.sinaedge.com,c=125.33.25.114;f=Edge,s=cnc.yizhuang.union.95,c=172.16.56.94
X-Via-Edge: 16136997797397219217d5e3810ac0a403fb9

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

-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 默認不跟隨重定向。常與-s連用

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

$ curl -L https://github.com/docker/compose/releases/download/1.25.0-rc4/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

--limit-rate

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

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

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

-o

-o參數將服務器的源代碼保存成文件,等同於wget命令

$ curl -o  文件名  https://www.example.com

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

-O

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

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

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

下載多個文件,如screen1-screen10

curl -O http://cgi2.tky.3web.ne.jp/~zzh/screen[1-10].JPG 

-s

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

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

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

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

$ cur
l -s -o /dev/null https://google.com

-S

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

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

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

-u

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

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

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

curl -u 'bob' https://google.com/login

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

-v

-v參數可以顯示一次 http 通信的整個過程,包括端口連接和 http request 頭信息。

 curl -v www.sina.com
*   Trying 123.125.104.150...
* TCP_NODELAY set
* Connected to www.sina.com (123.125.104.150) port 80 (#0)
> GET / HTTP/1.1
> Host: www.sina.com
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx
< Date: Fri, 19 Feb 2021 02:02:06 GMT
< Content-Type: text/html
< Content-Length: 22981
< Connection: keep-alive

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

$ curl --trace - https://www.example.com
$ curl --trace-ascii output.txt www.sina.com

-x

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

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

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

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

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

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

-X

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

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

$ curl -X GET -s http://127.0.0.1:2376/volumes | jq .
{
"Volumes": [
{
"CreatedAt": "2020-12-15T19:49:52+08:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/mnt/data/docker-data/volumes/pre_install_nvidia-docker/_data",
"Name": "pre_install_nvidia-docker",
"Options": {},
"Scope": "local"
}
],
"Warnings": null
}

其中jq “.”

最簡單的jq程序是表達式".",它不改變輸入,但可以將其優美地輸出,便於閱讀和理解。

https://www.jianshu.com/p/6de3cfdbdb0e    給力的linux命令--jq簡易教程,有待整理

 


http://blog.sina.com.cn/s/blog_4b9eab320100slyw.html#commonComment     curl命令使用大全

 







 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM