Reputation: 443
I have this Post request handler in an asp.net core webapi
[HttpPost()]
public IActionResult Post([FromBody]string value)
{
if (value == null)
{
return new HttpStatusCodeResult(400);
}
else
{
return new HttpStatusCodeResult(200);
}
}
I am trying post a string to it using HttpClient
from another app
using(var client = new HttpClient())
{
var content = new StringContent("test test");
var response = client.PostAsync("http://localhost:57316/api/values", content).Result;
var responseString = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(responseString);
}
the request gets received when I debug the handler above, but value is always null, I expect it to be "test test"
,waht am I doing wrong?
Upvotes: 2
Views: 940
Reputation: 155608
As you're not setting an explicit Content-Type
on the request, it's possible ASP.NET Web API is interpreting it as JSON, but as "test test
" (by itself, without quotes) is not valid JSON it will fail.
Try one of these:
Content-Type
header to "text/plain".new StringContent("\"test test\"");
Upvotes: 1