I'm trying to send json via POST using HttpClient to my webservice.
Send method is really simple:
HttpClient _httpClient = new HttpClient();
public async Task<HttpStatusCode> SendAsync(Data data)
{
string jsonData = JsonConvert.SerializeObject(data);
var content = new StringContent(
jsonData,
Encoding.UTF8,
"application/json");
HttpResponseMessage response = await _httpClient.PostAsync(_url, content);
return response.StatusCode;
}
On the server side I have the WebAPI controller with following method:
[HttpPost]
[ActionName("postdata")]
public async Task<HttpResponseMessage> PostData([FromBody] string jsonParam)
{
/// here the jsonParam is null when receiving from HttpClient.
// jsonParam gets deserialized, etc
}
The jsonParam in this method is null. The jsonData is good, if I copy and paste it into a request sender (I use Postman) everything is successful.
It's about how I construct the content and use the HttpClient but I can't figure out what's wrong.
Can anyone see the issue?