0 Comments

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";

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts