如何使用curl POST对url参数进行urlencode,但不对主体数据进行urlencode? [英] How to urlencode url params with a curl POST but not urlencode the body data?

查看:564
本文介绍了如何使用curl POST对url参数进行urlencode,但不对主体数据进行urlencode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用诸如之类的命令进行curl POST调用时| | curl -XPOST --data @-https://example.com/ 我也可以 在命令行中对某些URL参数进行URL编码(使用curl本身,而不是bash)

所以我希望POST正文不再是 em> urlencoded,但是url参数在同一curl命令中进行url编码。



例如:

  echo'{ username: a, password:[1,2]}'| curl --header内容类型:应用程序/ json --request POST -d @---data-urlencode filter =。 http:// localhost:8080 

似乎没有执行我想要的操作,并且我有后缀我的 https:// localhost:8080 / url和?filter ... 的URL,在某些情况下我宁愿避免该参数需要大量编码。

解决方案

您可以使用-data-urlencode 选项:

  ... | curl -s -X POST --data @---data-urlencode filter = ab& 123< url> 

根据 man curl


-data-urlencode< data>

( HTTP),此发布的数据类似于其他 -d -data 选项,但它执行URL -encoding。



When making a curl POST call with something like ... | curl -XPOST --data @- https://example.com/ can I also url-encode some url params on the command line (using curl itself, not bash funcs) without having to encode them myself and put them in the url argument to curl?

So I want the POST Body to not be urlencoded, but the url params to be url encoded within the same curl command.

So for example:

echo '{"username":"a","password": [1,2]}' | curl --header "Content-Type: application/json" --request POST -d@- --data-urlencode "filter=." http://localhost:8080

Doesn't seem to do what I want, and I have postfix my https://localhost:8080/ url with ?filter... which I would rather avoid for cases when the param needs a lot of encoding.

解决方案

You can use --data-urlencode option:

... | curl -s -X POST --data @- --data-urlencode "filter=ab&123" <url>

As per man curl:

--data-urlencode <data>
(HTTP) This posts data, similar to the other -d, --data options with the exception that this performs URL-encoding.

这篇关于如何使用curl POST对url参数进行urlencode,但不对主体数据进行urlencode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆