6

I'm trying to URL encode query parameters using curl with --data-urlencode but they end up being appended to the query like with --data.

The issue can be reproduced with the help of netcat and doing queries against it.

Example curl POST query:

curl --data-urlencode "foo=bar" 127.0.0.1:8080/test/path

Actual output:

$ nc -l 8080
POST /test/path HTTP/1.1
Host: 127.0.0.1:8080
User-Agent: curl/7.58.0
Accept: */*
Content-Length: 7
Content-Type: application/x-www-form-urlencoded

foo=bar

Expected output:

$ nc -l 8080
POST /test/path?foo=bar HTTP/1.1
Host: 127.0.0.1:8080
User-Agent: curl/7.58.0
Accept: */*

1 Answer 1

7

According to the manual, the --data-urlencode option "...posts data, similar to the other -d, --data options". So if you're seeing a post, that's the expected behavior.

You can use --get to make curl issue a GET request instead. Also, -v is a good friend!

curl -v --data-urlencode "foo=bar" http://127.0.0.1/test.php
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> POST /test.php HTTP/1.1
> Host: 127.0.0.1
> User-Agent: curl/7.47.0
> Accept: */*
> Content-Length: 7
> Content-Type: application/x-www-form-urlencoded

With --get:

curl -v --get --data-urlencode "foo=bar" http://127.0.0.1/test.php
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET /test.php?foo=bar HTTP/1.1
> Host: 127.0.0.1
> User-Agent: curl/7.47.0
> Accept: */*
Sign up to request clarification or add additional context in comments.

3 Comments

I later added explicit mention of that I want to do POST.
However, I do think your answer is correct and I had misunderstood that urlencoding meant encoding the data to the URL path but instead it means encoding the data in application/x-www-form-urlencoded to the body of the message.
Using the --get with --request POST has the desired effect of doing POST request with all the data encoded to the URL, but I have to say it feels quite confusing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.