1. curl發送get請求
curl http://11.120.12.89:6666/sengMsg?phone=18790987654\&name=lily&msg=aaa
注意:有多個參數時需要把&轉義一下,否則獲取不到之后參數會報錯
2. curl發送post請求
post請求類型application/x-www-form-urlencoded,使用-d參數以后,HTTP 請求會自動加上標頭Content-Type : application/x-www-form-urlencoded。並且會自動將請求轉為 POST 方法,因此可以省略-X POST。
curl http://11.120.12.89:6666/sengMsg -X POST -d "parameterName1=parameterValue1¶meterName2=parameterValue2"
-d參數可以讀取本地文本文件的數據,向服務器發送
$ curl -d '@data.txt' https://google.com/login
post請求類型application/json
curl http://11.120.12.89:6666/sengMsg -X POST -H "Content-Type:application/json" -d '{"parameterName1":"parameterValue1","parameterName2":"parameterValue2"}'
使用POST接口上傳文件,@后傳文件
curl http://11.120.12.89:6666/sengMsg -F raw=@/tmp/raw.data -v
參數 說明
-d 請求攜帶的參數,多個參數使用&分隔
-X 指定請求的方法,POST外還可以指定PUT等請求方法
-H 指定請求頭,例如 -H "Content-type:application/json"多個請求頭傳遞多-H即可
-v 顯示詳細的請求信息
-F 傳輸文件,指定 MIME 類型,指定文件名
-O 把輸出寫到該文件中,保留遠程文件的文件名
-u 通過服務端配置的用戶名和密碼授權訪問
-A 指定客戶端的用戶代理標頭,即User-Agent,curl 的默認用戶代理字符串是curl/[version]。
-b 向服務器發送 Cookie
-c 將服務器設置的 Cookie 寫入一個文件
-d 發送 POST 請求的數據體
–data-urlencode –data-urlencode參數等同於-d,發送 POST 請求的數據體,區別在於會自動將發送的數據進行 URL 編碼。
-e 設置 HTTP 的標頭Referer,表示請求的來源
-F 向服務器上傳二進制文件
-G 構造 URL 的查詢字符串
-i 打印出服務器回應的 HTTP 標頭
將下載的數據寫入到文件,必須使用文件的絕對地址:
curl https://www.linuxcool.com/abc.txt --silent -O
訪問需要授權的頁面時,可通過-u選項提供用戶名和密碼進行授權:
[root@linuxcool ~]# curl -u root https://www.linuxprobe.com/ Enter host password for user 'root':
-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
-b參數用來向服務器發送 Cookie,生成一個標頭Cookie: foo=bar,向服務器發送一個名為foo、值為bar的 Cookie。
讀取本地文件cookies.txt,里面是服務器設置的 Cookie
$ curl -b 'foo=bar' https://google.com $ curl -b 'foo1=bar' -b 'foo2=baz' https://google.com $ curl -b cookies.txt https://www.google.com
-c參數將服務器設置的 Cookie 寫入一個文件。將服務器的 HTTP 回應所設置 Cookie 寫入文本文件cookies.txt。
$ curl -c cookies.txt https://www.google.com
–data-urlencode參數等同於-d,發送 POST 請求的數據體,區別在於會自動將發送的數據進行 URL 編碼。發送的數據hello world之間有一個空格,需要進行 URL 編碼。
$ curl --data-urlencode 'comment=hello world' https://google.com/login
-e參數用來設置 HTTP 的標頭Referer,表示請求的來源。將Referer標頭設為https://google.com?q=example。
curl -e 'https://google.com?q=example' https://www.example.com
-H參數可以通過直接添加標頭Referer,達到同樣效果。
curl -H 'Referer: https://google.com?q=example' https://www.example.com
-F參數用來向服務器上傳二進制文件。給 HTTP 請求加上標頭Content-Type: multipart/form-data,然后將文件photo.png作為file字段上傳。
$ curl -F 'file=@photo.png' https://google.com/profile
-F參數可以指定 MIME 類型。指定 MIME 類型為image/png,否則 curl 會把 MIME 類型設為application/octet-stream。
$ curl -F 'file=@photo.png;type=image/png' https://google.com/profile
-F參數也可以指定文件名。原始文件名為photo.png,但是服務器接收到的文件名為me.png。
$ curl -F 'file=@photo.png;filename=me.png' https://google.com/profile
-G參數用來構造 URL 的查詢字符串。上面命令會發出一個 GET 請求,實際請求的 URL 為https://google.com/search?q=kitties&count=20。如果省略–G,會發出一個 POST 請求。
$ curl -G -d 'q=kitties' -d 'count=20' https://google.com/search
如果數據需要 URL 編碼,可以結合–data–urlencode參數。
$ curl -G --data-urlencode 'comment=hello world' https://www.example.com
-i參數打印出服務器回應的 HTTP 標頭。命令收到服務器回應后,先輸出服務器回應的標頭,然后空一行,再輸出網頁的源碼。
$ curl -i https://www.example.com
-I參數向服務器發出 HEAD 請求,然會將服務器返回的 HTTP 標頭打印出來。令輸出服務器對 HEAD 請求的回應。–head參數等同於-I。
$ curl -I https://www.example.com $ curl --head https://www.example.com
-k參數指定跳過 SSL 檢測。
$ curl -k https://www.example.com
上面命令不會檢查服務器的 SSL 證書是否正確。
-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
參考:https://blog.csdn.net/qq_35456400/article/details/108790518