I'm trying to post JSON data to web api, both projects run on my local machine.
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(@"http://localhost:53818/");
var result = client.PostAsync("api/values", new StringContent(data, Encoding.UTF8, "application/json")).Result;
string resultContent = result.Content.ReadAsStringAsync().Result;
Console.WriteLine(resultContent);
}
Received values in This post method is NULL
public HttpResponseMessage Post([FromBody]string value)
{
return new HttpResponseMessage(HttpStatusCode.Created);
}
EDIT ----------- So I managed to figure out what the issue was. I've substituted this line of code
client.PostAsync("api/values", new StringContent(data, Encoding.UTF8, "application/json")).Result;
with the following and it worked, if someone will post the explanation I will be grateful
var response = client.PostAsJsonAsync("api/values", data).Result;