25 October, 2013
0 Comments
1 category
When posting UTF8 JSON data with the WebClient.UploadString function, I was getting NULL as value for the parameter on the controller function, the simple fix:
using (var client = new WebClient()) { // Set the header so it knows we are sending JSON. client.Headers[HttpRequestHeader.ContentType] = "application/json"; client.Encoding = UTF8Encoding.UTF8; // …
string responseData = client.UploadString("http://localhost/service", utf8RequestData); // Deserialise the response into a object var outputDTO = JsonConvert.DeserializeObject<GenerateDocumentOutputDto>(responseData); }
Or you could change the ContentType:
client.Headers[HttpRequestHeader.ContentType] = "application/json; charset=utf-8";
Category: Uncategorized