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 work around.
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.