curl 常用参数备忘

  |   0 评论   |   2,067 浏览

参数名作用备忘
I只显示文档信息等价于 --head
i在输出中显示协议头Include protocol headers in the output (H/F)
-k, --insecure忽略证书问题等安全校验
-v, --verbose显示请求过程详细信息Useful for debugging and seeing what's going on "under the hood".
-F, --form模拟提交一个表单(HTTP SMTP IMAP) For HTTP protocol family, this lets curl emulate a filled-in form in which a user has pressed the submit button. This causes curl to POST data using the Content-Type multipart/form-data according to RFC 2388.

常用命令:

上传文件

curl --form upload=@/path/to/filename.xxx    \
     --form formParamName1=value             \
     --form formParamName2=value             \
     http://localhost:8080/upload

注: 上面的命令实际是生成一个POST FORM参数. 关键步骤是会生成一个表单参数: multipart/form-data

我们看一个如下的实例:

curl -v  --form a=b www.baidu.com
*   Trying 103.235.46.39...
* TCP_NODELAY set
* Connected to www.baidu.com (103.235.46.39) port 80 (#0)
> POST / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.64.1
> Accept: */*
> Content-Length: 137
> Content-Type: multipart/form-data; boundary=------------------------e3141fd1210883ec
>
* We are completely uploaded and fine
< HTTP/1.1 302 Found
< Connection: keep-alive
< Content-Length: 17931
< Content-Type: text/html
< Date: Sat, 23 Apr 2022 10:05:37 GMT
< Etag: "54d9748e-460b"
< Server: bfe/1.0.8.18
<

也可以使用 -F 参数进行简写.

curl -F 'paramA=valueA' -F 'filesName=@/path/to/file'

参数-g

可能的报错: # curl: (3) [globbing] bad range specification in column 107 错误。

#       -g, --globoff
              This option switches off the "URL globbing parser". When you set this option, you can specify URLs that contain the letters  {}[]  without  having  them
              being interpreted by curl itself. Note that these letters are not normal legal URL contents but they should be encoded according to the URI standard.

# 释义:  选项开关 - 文件名扩展解析器. 当你设置了此标记的时候,你可以指定包含字母:{}和[]的URL. 这些字母不会被curl再次解析. 
             注意,这些字母不是正常的合法URL内容.

当你的url里面包含有一些特殊字符的时候,你又不想处理.这个时候可以用这个参数进行暂时处理. 正常的处理方法还是进行编码后使用. 比如下面这个是一个不好的实践:

curl -H "Host: xyks.yuanfudao.xxx" \
-H "Cookie: sid=99374083520958116; YFD_U=-6654511933970064481; wxsess=YOa1rlCiJyikgfoRyjQGpAgQENT8oDHNb3ElbtGe3eSnzDdiguHX0hALRV9LJI329dU8Smfy2/vQe86rjtIjzN164ZCOkHjy8IQA9tikA4Y=; userid=201703736; __sub_user_infos__=/24T7Ovgtuud9LSYuz4IE3TWmK65CT7S5el8cBskXKkjy969eCCcUWWQt0GmGR3SPPUIVPF8dMH/wQ8WHE+8Mw==; g_loc=vPeteFfrRL2jJ0VNIL3TWQ==; persistent=8ACna01r3yKDLPHqUrfb1j+reOFAIpXOCCC1PE2wIbdOBRQ0jIehzt+dDK9I0R1uECpgtFnmeCAE9QDgveglNQ==; sess=Gk5qkVX45GynE8blqtX3LwDTgsDp3WKFJP9oebNHoieid65fgfo2I1JdK1Rjl/UtmTo/MtxM6TrteIXnhXIYJ8L/NCYy31sNuLwSuhfcHHw=; g_sess=Gk5qkVX45GynE8blqtX3LwDTgsDp3WKFJP9oebNHoieid65fgfo2I1JdK1Rjl/UtmTo/MtxM6TrteIXnhXIYJ4kJ24Br6dONs3lH674UGxYLjBttPnfvFVLwR9r005dM" \
-H "accept: application/json, text/plain, */*" \
-H "user-agent: Mozilla/5.0 (Linux; Android 10; CLT-AL01 Build/HUAWEICLT-AL01; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/86.0.4240.99 XWEB/3263 MMWEBSDK/20210501 Mobile Safari/537.36 MMWEBID/7156 MicroMessenger/8.0.6.1900(0x28000653) Process/toolsmp WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64" \
-H "x-requested-with: com.tencent.mm" \
-H "sec-fetch-site: same-origin" \
-H "sec-fetch-mode: cors" \
-H "sec-fetch-dest: empty" \
-H "referer: https://xyks.yuanfudao.biz/h5/leo-web-one-yuan-purchase/one-yuan-2022.html" \
-H "accept-language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7" \
--compressed "https://xyks.yuanfudao.biz/leo-goblin-mall/api/order/confirm/getOrderInfo?leoBizType=70&purchaseGoodsVOs=[%7B%22leoVariantId%22:+8093,+%22purchaseCount%22:+1%7D]&_productId=631&_appId=0"

参考

  1. 使用 CURL 上传文件
  2. [Extracted fromthis excellent curl tutorial](https://gist.github.com/joyrexus/8ea8fe71932dda48e554)
  3. 关于curl命令发送get请求时需要编码的问题

评论

发表评论


取消