curl设置header和表单数据

老是分不清楚header和表单时的写法,每次都去翻别人的博客

从浏览器中获取某个请求的curl版本

curl设置header和表单数据_第1张图片
下面是几种上述方法拷贝出来的curl命令参数,特意列出来,供参考一下。

设置header与表单

GET请求

curl 'http://xxxxxxxxx/vehicles?page=1&size=20&number=8' \
-H 'Accept-Encoding: gzip, deflate' \
-H 'Accept-Language: zh,en;q=0.9,ja;q=0.8,zh-TW;q=0.7,fr;q=0.6,zh-CN;q=0.5' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36' \
-H 'Accept: application/json, text/plain, */*' \
-H 'Referer: http://xxxxxxxxx/' \
-H 'userId: 454' \
-H 'Connection: keep-alive' \
-H 'token: af0d8773933eaeffc81d97924bd2a8fc' \
--compressed

POST请求,发送body信息
--data-binary DATA HTTP POST binary data (H)

curl 'http://xxxxxxxxx/nemt/nemt/order/cancel' 
-H 'Origin: http://xxxxxxxxx' 
-H 'Accept-Encoding: gzip, deflate' 
-H 'Accept-Language: zh,en;q=0.9,ja;q=0.8,zh-TW;q=0.7,fr;q=0.6,zh-CN;q=0.5' 
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36' 
-H 'Content-Type: application/json' 
-H 'Accept: application/json, text/plain, */*' 
-H 'Referer: http://xxxxxxxxx/' 
-H 'userId: 454' 
-H 'Connection: keep-alive' 
-H 'token: af0d8773933eaeffc81d97924bd2a8fc' 
--data-binary '{"id":"10076","reasonId":0}' 
--compressed

POST请求,发送表单
--data DATA HTTP POST data (H)

# 此例来自:http://www.ruanyifeng.com/blog/2011/09/curl.html
curl -X POST --data "data=xxx" example.com/form.cgi

文件上传

# 此例来自项目中某安卓端代码
do_upload(){
    curl -X POST -F "file=@$1" $2
}
# 此例来自:http://www.ruanyifeng.com/blog/2011/09/curl.html
curl --form upload=@localfilename --form press=OK [URL]

OK,清理书签完毕!

你可能感兴趣的:(Linux)