Reputation: 30388
I'm trying to read my employees list from a Web API call but getting errors.
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://myApiUrl");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("api/hr/employees");
if (response.IsSuccessStatusCode)
{
List<Employee> employees = response.Content.ReadAsAsync(IEnumerable<Employee>).Result;
}
}
The error I'm currently getting is: System.Net.Http.HttpContent does not contain a definition for ReadAsAsync...
What am I doing wrong?
Upvotes: 2
Views: 2381
Reputation: 1
Try
List<Employee> employees = response.Content.ReadAsAsync<IEnumerable<Employee>>().Result;
Upvotes: 0
Reputation: 1806
ReadAsAsync is not a method on the HttpClient class. If you want to read the data and cast it to an object you can use:
HttpResponseMessage response = await client.GetAsync("api/hr/employees");
if (response.IsSuccessStatusCode)
{
var result= await response.Content.ReadAsStringAsync();
List<Employee> employees = JsonConvert.DeserializeObject<Employee>(result);
}
Upvotes: 3