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

Viewed 8949

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 Answers

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.

Related