I am trying to send json via POST using HttpClientin my web service.
Submit 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 a WebAPI controller with the following method:
[HttpPost]
[ActionName("postdata")]
public async Task<HttpResponseMessage> PostData([FromBody] string jsonParam)
{
}
jsonParam in this method null. jsonDataItβs good if I copy and paste it into the request sender (I use Postman), everything will be successful.
It's about how I construct content and use HttpClient, but I canβt understand what happened.
Can anyone see the problem?
source
share