1

When using cURL to send data via POST, if that data string is URL encoded or if parts of it are URL encoded, cURL automatically decodes the data when sending it.

This happens when using cURL in PHP or directly in the command line.

I've tested with 2 different version of cURL: 7.19 and 7.49. Both exhibit the same behavior

I've sent the cURL request from two different servers thinking that the the way the servers was configured somehow influenced this, but the result was the same.

Here is a simple PHP cURL request that I've used for my test:

$ua = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13';

$data = "https%3A%2F%2Fexample.com%3A8081%2Ftemoignez%3FQid%3D%26"       

$ch = curl_init( "https://example.com/test/webhook.php" );
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "payload=".$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec( $ch );

The data will be sent decoded even though the initial string is URL encoded.

I'm retrieving the data by dumping the POST data into a file on disk using PHP.

Is this normal? Any idea what may cause this?

9
  • Why do you not want it decoded? Commented Sep 26, 2016 at 11:50
  • That is irrelevant to my question, it is project specific that the URLs are to be encoded Commented Sep 26, 2016 at 11:51
  • 1
    $response = curl_exec( $ch ); is probably why curl is decoding the url I'm not sure how you would disable it (if its possible) short of searching php.net under Curl and see if there is an option to turn that part off. I'm interested to find that out as well Commented Sep 26, 2016 at 12:16
  • 1
    How did you test it whether it was decoded? Commented Sep 26, 2016 at 12:17
  • 2
    Wouldn't reading from php://input instead of $_POST actually be a solution to this problem? Commented Sep 26, 2016 at 13:24

1 Answer 1

3

You have two different assertions here:

cURL automatically decodes the data when sending it.

...

I've simply dumped the POST data into a file after retrieving it.

It is PHP that automatically DECODES the data when receiving it. It is NOT getting decoded upon sending it!

This integrates with the behaviour of other values, like cookie data, post and get variables, header information like referrer, ... everything get's decoded automatically when it is received, because it is expected to be sent encoded.

When you want to see the exact data that is getting send over the wires, use a tool like ngrep on port 80 to sniff the TCP HTTP traffic.

Sign up to request clarification or add additional context in comments.

6 Comments

Then how come URL encoded data sent with Postman and Hurl.it remains encoded when received?
@Remus Triple check what Postman actually sends. It's probably encoding your data for you, so you're sending it double-encoded.
@deceze I've checked with Postman documentation and it specifically mentions that parameters entered in the URL bar or in the key/value editor will not automatically be URL-encoded
@deceze I've also double checked with Charles Proxy. Anyway to triple check? :)
@Remus You'll have to provide a compelling demonstration. Fact is that data in $_POST is decoded by PHP. Your example with curl behaves exactly as one would expect.
|

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.