Curl 命令和用法
生活随笔
收集整理的這篇文章主要介紹了
Curl 命令和用法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1. 簡介
curl 是常用的命令行工具,用來請求 Web 服務(wù)器。它的名字就是客戶端(client)的 URL 工具的意思。
它的功能非常強(qiáng)大,命令行參數(shù)多達(dá)幾十種。如果熟練的話,完全可以取代 Postman 這一類的圖形界面工具。
2. 命令
2.1 -A 和-H
# 不帶有任何參數(shù)時,curl 就是發(fā)出 GET 請求。 $ curl https://www.example.com# -A參數(shù)指定客戶端的用戶代理標(biāo)頭,即User-Agent。curl 的默認(rèn)用戶代理字符串是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標(biāo)頭。 $ curl -A '' https://google.com # -H參數(shù)直接指定標(biāo)頭,更改User-Agent $ curl -H 'User-Agent: php/1.0' https://google.com2.2 -b 參數(shù)用來向服務(wù)器發(fā)送 Cookie
$ curl -b 'foo=bar' https://google.com # 生成一個標(biāo)頭Cookie: foo=bar,向服務(wù)器發(fā)送一個名為foo、值為bar的 Cookie。$ curl -b 'foo1=bar;foo2=bar2' https://google.com # 發(fā)送兩個 Cookie $ curl -b cookies.txt https://www.google.com # 讀取本地文件cookies.txt,里面是服務(wù)器設(shè)置的 Cookie(參見-c參數(shù)),將其發(fā)送到服務(wù)器。2.3 -c參數(shù)將服務(wù)器設(shè)置的 Cookie 寫入一個文件
$ curl -c cookies.txt https://www.google.com 上面命令將服務(wù)器的 HTTP 回應(yīng)所設(shè)置 Cookie 寫入文本文件cookies.txt。2.4 -d 參數(shù) 和 --data-urlencode
# -d參數(shù)用于發(fā)送 POST 請求的數(shù)據(jù)體。 $ 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參數(shù)以后,HTTP 請求會自動加上標(biāo)頭Content-Type : application/x-www-form-urlencoded。并且會自動將請求轉(zhuǎn)為 POST 方法,因此可以省略-X POST。# -d參數(shù)可以讀取本地文本文件的數(shù)據(jù),向服務(wù)器發(fā)送 $ curl -d '@data.txt' https://google.com/login 上面命令讀取data.txt文件的內(nèi)容,作為數(shù)據(jù)體向服務(wù)器發(fā)送。# --data-urlencode參數(shù)等同于-d,發(fā)送 POST 請求的數(shù)據(jù)體,區(qū)別在于會自動將發(fā)送的數(shù)據(jù)進(jìn)行 URL 編碼。 $ curl --data-urlencode 'comment=hello world' https://google.com/login 上面代碼中,發(fā)送的數(shù)據(jù)hello world之間有一個空格,需要進(jìn)行 URL 編碼。2.5 -H參數(shù)添加 HTTP 請求的標(biāo)頭
$ curl -H 'Accept-Language: en-US' https://google.com 上面命令添加 HTTP 標(biāo)頭Accept-Language: en-US。$ curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com 上面命令添加兩個 HTTP 標(biāo)頭。$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login 上面命令添加 HTTP 請求的標(biāo)頭是Content-Type: application/json,然后用-d參數(shù)發(fā)送 JSON 數(shù)據(jù)。2.6 -F參數(shù)用來向服務(wù)器上傳二進(jìn)制文件。
$ curl -F 'file=@photo.png' https://google.com/profile 上面命令會給 HTTP 請求加上標(biāo)頭Content-Type: multipart/form-data,然后將文件photo.png作為file字段上傳。-F參數(shù)可以指定 MIME 類型。$ curl -F 'file=@photo.png;type=image/png' https://google.com/profile 上面命令指定 MIME 類型為image/png,否則 curl 會把 MIME 類型設(shè)為application/octet-stream。-F參數(shù)也可以指定文件名。$ curl -F 'file=@photo.png;filename=me.png' https://google.com/profile 上面命令中,原始文件名為photo.png,但是服務(wù)器接收到的文件名為me.png。2.7 -k參數(shù)指定跳過 SSL 檢測。
$ curl -k https://www.example.com 上面命令不會檢查服務(wù)器的 SSL 證書是否正確。2.8 -L參數(shù)會讓 HTTP 請求跟隨服務(wù)器的重定向。curl 默認(rèn)不跟隨重定向
$ curl -L -d 'tweet=hi' https://api.twitter.com/tweet總結(jié)
以上是生活随笔為你收集整理的Curl 命令和用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2020数学建模A-炉温曲线
- 下一篇: 题目1072 括号匹配问题