Home > Back-end >  Get request to github repos using httpClient?
Get request to github repos using httpClient?

Time:03-14

I am trying to get a list of repos from api repos endpoint, however i am having issues with implementing this in a method. The error i get states that it cannot convert IEnumerable RepoData to IEnumerable RepoData. Here is what I have so far:

public class RepoResult
{
    public IEnumerable<RepoData> RepoData { get; set; }
}

public class RepoData
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Html_Url { get; set; }
}


public async Task<IEnumerabe<RepoData>> LoadRepos()
{
        string url = "https://api.github.com/users/jeremyolu/repos";

        using (HttpResponseMessage response = await _apiClient.GetAsync(url))
        {
            if (response.IsSuccessStatusCode)
            {
                RepoResult result = await response.Content.ReadAsAsync<RepoResult>();

                return result.RepoData;
            }
            else
            {
                throw new Exception(response.ReasonPhrase);
            }
        }
 }

CodePudding user response:

Instead of defining the RepoResult class just do this:

if (response.IsSuccessStatusCode)
{
    return JsonConvert.DeserializeObject<IEnumerable<RepoData>>(await response.Content.ReadAsStringAsync());
}
  • Related