I am making of System.Net.Http.HttpClient
class to call an end point. This endpoint is expect certain input and returns a List of user defined object of type Employee ( List<Employee>)
.
This is the code that I am using.
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:7792/");
client.DefaultRequestHeaders.Accept.Clear();
FilterModel payload = new FilterModel();
payload.employeeId= 97050001;
payload.Level= "Manager";
// New code:
HttpResponseMessage response = client.PostAsJsonAsync("api/employee", payload).Result;
if (response.IsSuccessStatusCode)
{
var employee= response.Content.ReadAsStringAsync();
//HOW DO I CONVERT THE OUTPUT INTO LIST<EMPLOYEE>?
Console.Write("---DONE---");
}
Console.ReadKey();
}
I know this is not the ideal way to call an end point & I must use asyc await. I just need the data, the call can be sync or async & I want to type cast the result into List.
Currently I get a string back which I need to deserialize. Please help
You can use JsonConvert as below:
var jsonString= response.Content.ReadAsStringAsync();
var employees = JsonConvert.DeserializeObject<List<Employee>>(jsonString);
Hope this help!