HttpContent ReadAsAsync with .NET Core 2

Full source code available here.

If you are used to using HttpContent.ReadAsAsync you might be surprised to learn that it is missing from .NET Core 2. You can try adding Microsoft.AspNet.WebApi.Client but you might get warnings or errors.

At some point Microsoft will come out with an updated NuGet package, but in the meantime here is a workaround.

At this extension method to your code.

using Newtonsoft.Json;
using System.Net.Http;
using System.Threading.Tasks;

namespace ReadAsAsyncCore
    public static class HttpContentExtensions
        public static async Task<T> ReadAsJsonAsync<T>(this HttpContent content)
            string json = await content.ReadAsStringAsync();
            T value = JsonConvert.DeserializeObject<T>(json);
            return value;

And use like this.

HttpResponseMessage httpResponse = await httpClient.GetAsync(requestEndpoint);
List<Product> products = await httpResponse.Content.ReadAsJsonAsync<List<Product>>();

Full source code available here.

7 thoughts on “HttpContent ReadAsAsync with .NET Core 2

  1. Hi Bryan,
    thank you very much for the code, it did help me today.

    There is a little typo in there though that you might want to fix. In your code the extension is called “ReasAsJsonAsync” instead of “ReadAsJsonAsync”.


  2. With .NET Core 3.1 this is what I am finding. Regarding the static method…


    The above works but it is documented on a webpage under “no longer updating this content regularly. Check the Microsoft Product Lifecycle”.

    The instance method also works but I can’t find any Microsoft description of it.


    • Hi Brian,

      What page are you referring to?

      .NET Core 3.1 has this method and you should not need to use the extension described here.


Leave a Reply

Your email address will not be published. Required fields are marked *