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 $_POST
variable 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();
?>