How to urlencode url params with a curl POST but not urlencode the body data?

你离开我真会死。 提交于 2020-08-10 19:42:32

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/52334832/how-to-urlencode-url-params-with-a-curl-post-but-not-urlencode-the-body-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!