介紹
curl是底層使用 libcurl 庫開發(fā)的命令行工具,是一個網(wǎng)絡數(shù)據(jù)傳輸項目,它支持 DICT、FILE、FTP、FTPS、Gopher、HTTP、HTTPS、IMAP、IMAPS、LDAP、LDAPS、POP3、POP3S、RTMP、RTSP、SCP、SFTP、SMB、SMBS、SMTP、SMTPS、Telnet 與 TFTP 等協(xié)議,功能也比較豐富,今天筆者就一起來為大家解鎖更多用法吧
常規(guī)用法
說明: http://api.com是url地址
Get請求
curl http://api.com
# 攜帶參數(shù)
curl http://api.com?name=參數(shù)
Post請求
# 1.普通表單請求
# -d 后面跟參數(shù)
curl -X POST -d 'username=zhangs&pass=123456' http://api.com
# 2.JSON數(shù)據(jù)傳參
# -H 設置頭信息 -d 后面跟參數(shù)
curl -X POST http://api -H "Content-type: application/json" -d'{"phone":"13111111111", "smsCode":"124567"}'
-X 指定請求類型
-H 指定請求頭(若有多個請求頭,可以 -H "XX:XX" -H "XX:XX" ......)
-d 參數(shù)
-F 指定文件
上傳文件
curl -F "file=@a.txt" http://api.com
# a.txt是文件
下載文件
curl http://api.com -o a.txt
# a.txt是文件
--progress 顯示進度信息
其他用法參數(shù)說明
-A:用戶代理頭 User-Agent
eg: curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100" http://api.com-b:使用cookie
eg:curl -b "cookie=124" http://api.com
eg:curl -b cookie.txt http://api.com-c:存儲服務器返回cookie
eg:curl -c cookie_save.txt http://api.com-d:參數(shù)
eg:curl -X POST -d "username=zhangs&age=18" http://api.com-e:設置http的Referer
eg:curl -e 'xxx' http://api.com-H :設置請求頭
eg:curl -H 'xxx' http://api.com-i:打印印出服務器回應的 HTTP 標頭
eg:curl -i http://api.com
-k:忽略ssl檢測
eg:curl -k https://api.com-o:保存成文件
eg:curl -o save.txt https://api.com-u:設置服務認證需要的用戶名密碼
eg:curl -u 'username:password' https://api.com-
-x:設置網(wǎng)絡代理
eg:socks5代理
curl -x socks5://username:password@127.0.0.1:8080 https://api.com
eg:http代碼
curl -x username:password@127.0.0.1:8080 https://api.com -X(大寫):指定請求方式
eg:curl -X POST https://api.com
更詳細用法可參考 cookbooks