A day with .Net

My day to day experince in .net

HttpClient.PostAsJsonAsync and HttpContent.ReadFromJsonAsync Json extensions

Posted by vivekcek on December 11, 2020

If you have configured your json serialization options in Startup.cs. You can use HttpClient and HttpContent’s json extension methods to send and parse http request as below.

public async Task<MyResponse> Call(MyRequest request)
{
     var response = await _httpClient.PostAsJsonAsync(Url, request);
     response.EnsureSuccessStatusCode();
     return await response.Content.ReadFromJsonAsync<MyResponse>();
 }

Leave a comment