I am trying to call this API using the async await style but having some issues.
public async Task<User> GetUser(int userId)
{
User user = null;
HttpResponseMessage response = await client.GetAsync("/user/...");
if(response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
user = JsonConvert.DeserializeObject<User>(result);
}
return user;
}
I am calling the above method in my console app:
var user = apiClient.GetUser(123);
Console.WriteLine($"user result is {user.Result.UserId}");
I get the error:
An unhandled exception of type 'System.NullReferenceException Object reference not set to an instance..
Is my code snippet following the best practise is there something wrong?
My endpoint works fine when I test using Postman.
Update
The interface for my GetUser method is:
Task<User> GetUser(int userId);