0

My server works with PHP and use $_POST variable. My Client use C#.

With ContentType = application/json, i need to use $post = json_decode(file_get_contents('php://input'), true); in PHP server

But with ContentType = application/x-www-form-urlencoded the $_POSTvariable is a array with the json string like [ **the json variable as string** ]

What is wrong with my code when i use application/x-www-form-urlencoded


Exemple:

var post = new Dictionary<string, object>
{
    ["key1"] = "data1",
    ["key2"] = new List<string>{ "data2", "data3" }
};

Get [ "[\"key1\"] = \"data1\" ... \"data3\"]" ] as $_POST (1 array with 1 string)


The C# client:

var post = new Dictionary<string, object>
{
    ["key1"] = "data1",
    ["key2"] = new List<string>{ "data2", "data3" }
};
var url = "https://urlrandom.com";
var method = "POST";

using (var webClient = new WebClient())
{
    webClient.Headers[HttpRequestHeader.UserAgent] = "Other";
    webClient.Headers[HttpRequestHeader.Accept] = "application/json";
    webClient.Headers[HttpRequestHeader.ContentType] = ; // "application/json" or "application/x-www-form-urlencoded"

    var dataString = JsonConvert.SerializeObject(post);
    var bytes = Encoding.UTF8.GetBytes(dataString);
    var jsonBytes = webClient.UploadData(url, method, bytes);
    var jsonString = Encoding.UTF8.GetString(jsonBytes);
    var json = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);

    return json;
}

The PHP server:

<?php 
echo array_key_exists($_POST, "key1"); /* false with application/x-www-form-urlencoded */
exit();
?>
Rod
  • 712
  • 2
  • 12
  • 36
  • are you decoding the JSON on PHP server? – Dylan Feb 25 '19 at 22:08
  • It is not clear what issue you are facing with C# code. Can explain a bit more ? – Chetan Feb 25 '19 at 22:10
  • https://stackoverflow.com/questions/18866571/receive-json-post-with-php – ADyson Feb 25 '19 at 22:20
  • P.S. `application/x-www-form-urlencoded` is not that hard to understand...you just put the data in a querystring format, e.g. `param=x&param2=42&param3=abc` ...etc. It gets a bit more involved if you have a complex object but the general concept isn't tricky. – ADyson Feb 25 '19 at 22:21
  • I'm not sure if `UploadData` set `ContentLengh` to the correct value. You may need to explicitly set content length to let the client and server know how much data you upload. – Max Feb 25 '19 at 22:23
  • I don't think PHP will decode json input automatically. Check https://stackoverflow.com/questions/18866571/receive-json-post-with-php/18867369#18867369 for how you can do this manually – apokryfos Feb 25 '19 at 22:38
  • Please look my edit, thanks for your help – Rod Feb 26 '19 at 11:41
  • "What is wrong with my code when i use application/x-www-form-urlencoded" ... What's wrong is that you're telling the server you're going to send data in one format and then sending data in a different format. The server is confused by this. It tries to interpret the data in the url-encoded format, but ends up with nonsense because what you actually sent was JSON instead. – ADyson Feb 27 '19 at 02:43
  • If you're going to send data in JSON format then set the content type header to "application/json". If you're going to send data in url-encoded format then set the content type header to "application/x-www-form-urlencoded". Then the server can check the header and know how to read the data correctly. I hope the logic of this is fairly obvious. Of course the server needs to have code written to run this test and decide what to do. alternatively the server can choose to only accept data in one specific format, and throw and error if the input data does not match that. – ADyson Feb 27 '19 at 02:46
  • In your case you are sending JSON, but then writing PHP code which tries to read the data like it is url-encoded (regardless of the content type header, since you didn't check it's value). If you want to send JSON then in the PHP you have to read the JSON using json_decode(file_get_contents('php://input'), true); as you already mentioned yourself. After that you can then use the object returned by that code as your data in PHP. PHP does not put JSON data into the normal $_POST variable, so searching in there will never work. But you know how to read the JSON...so what's the problem?? – ADyson Feb 27 '19 at 02:55

0 Answers0