Home > Back-end >  cannot convert from 'System.Threading.Tasks.Task<string>' to 'string'
cannot convert from 'System.Threading.Tasks.Task<string>' to 'string'

Time:12-29

I am fetching data from API and trying to convert it into deserialized form. But gettting this error.

public static async Task<List<Movie>> GetAllMovies(int pageNumber, int pageSize)
        {
            var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", Preferences.Get("accessToken", string.Empty));
            var response = httpClient.GetStringAsync(AppSettings.ApiUrl   String.Format("api/movies/AllMovies?sort=asc&pageNumber={0}&pageSize={1}",pageNumber,pageSize));
            return JsonConvert.DeserializeObject<List<Movie>>(response);

        }

CodePudding user response:

Use await before response

  return JsonConvert.DeserializeObject<List<Movie>>(await response)

CodePudding user response:

Change your method to :

public static async Task<List<Movie>> GetAllMovies(int pageNumber, int pageSize)
{
    var httpClient = new HttpClient();
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", Preferences.Get("accessToken", string.Empty));
    var result = httpClient.GetStringAsync(AppSettings.ApiUrl   String.Format("api/movies/AllMovies?sort=asc&pageNumber={0}&pageSize={1}",pageNumber,pageSize));
    var response = await result.Content.ReadAsStringAsync();
    return JsonConvert.DeserializeObject<List<Movie>>(response);
}
  • Related