I’m trying to make a post to my web api service. The point is that while sending a message like
{ message: "it is done" }
working fine. However when I use special characters like çıöpş in my message it is unable to convert my json so that the post object remains null. What can I do? It is either current culture issue or something else. I tried to send my post parameter as HtmlEncoded style with encoding HttpUtility class but it didn’t work either.
public class Animal{
public string Message {get;set;}
}
Web API method
public void DoSomething(Animal a){
}
Client
Animal a = new Animal();
a.Message = "öçşistltl";
string postDataString = JsonConvert.SerializeObject(a);
string URL = "http://localhost/Values/DoSomething";
WebClient client = new WebClient();
client.UploadStringCompleted += client_UploadStringCompleted;
client.Headers["Content-Type"] = "application/json;charset=utf-8";
client.UploadStringAsync(new Uri(URL), "POST",postDataString);
Best regards,
Kemal
One possibility is to use the UploadDataAsync method which allows you to specify UTF-8 when encoding the data because the
UploadStringAsyncmethod that you are using basically usesEncoding.Defaultto encode the data when writing it to the socket. So if your system is configured to use some other encoding than UTF-8 you get into trouble becauseUploadStringAsyncuses your system encoding whereas in your content type header you have specifiedcharset=utf-8which could be conflicting.With the
UploadDataAsyncmethod you could be more explicit in your intents:Another possibility is to specify the encoding of the client and use
UploadStringAsync:Or if you install the
Microsoft.AspNet.WebApi.ClientNuGet package on the client you could directly use the newHttpClientclass (which is the new kid on the block) to consume your WebAPI instead ofWebClient: