2

I am trying to figure out how to use the Refit library to make GET requests but I do not know why it is not working. I am following the example on the refit github page. What am I missing? It seems like the GetUser("octocat") method is not working. I've tried searching for other examples on how to use refit but wasn't able to find anything.

    private static async void getUser()
    {

        var gitHubApi = RestService.For<IGitHubApi>("https://api.github.com");


        var octocat = await gitHubApi.GetUser("octocat");

}

public class User
{

    [JsonProperty("login")]
    public string Login { get; set; }

    [JsonProperty("id")]
    public long Id { get; set; }

    [JsonProperty("avatar_url")]
    public string AvatarUrl { get; set; }

    [JsonProperty("gravatar_id")]
    public string GravatarId { get; set; }

    [JsonProperty("url")]
    public string Url { get; set; }

    [JsonProperty("html_url")]
    public string HtmlUrl { get; set; }

    [JsonProperty("followers_url")]
    public string FollowersUrl { get; set; }

    [JsonProperty("following_url")]
    public string FollowingUrl { get; set; }

    [JsonProperty("gists_url")]
    public string GistsUrl { get; set; }

    [JsonProperty("starred_url")]
    public string StarredUrl { get; set; }

    [JsonProperty("subscriptions_url")]
    public string SubscriptionsUrl { get; set; }

    [JsonProperty("organizations_url")]
    public string OrganizationsUrl { get; set; }

    [JsonProperty("repos_url")]
    public string ReposUrl { get; set; }

    [JsonProperty("events_url")]
    public string EventsUrl { get; set; }

    [JsonProperty("received_events_url")]
    public string ReceivedEventsUrl { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("site_admin")]
    public bool SiteAdmin { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("company")]
    public string Company { get; set; }

    [JsonProperty("blog")]
    public string Blog { get; set; }

    [JsonProperty("location")]
    public string Location { get; set; }

    [JsonProperty("email")]
    public object Email { get; set; }

    [JsonProperty("hireable")]
    public object Hireable { get; set; }

    [JsonProperty("bio")]
    public object Bio { get; set; }

    [JsonProperty("public_repos")]
    public long PublicRepos { get; set; }

    [JsonProperty("public_gists")]
    public long PublicGists { get; set; }

    [JsonProperty("followers")]
    public long Followers { get; set; }

    [JsonProperty("following")]
    public long Following { get; set; }

    [JsonProperty("created_at")]
    public System.DateTimeOffset CreatedAt { get; set; }

    [JsonProperty("updated_at")]
    public System.DateTimeOffset UpdatedAt { get; set; }
}

interface IGitHubApi
{


    [Get("/users/{user}")]
    Task<User> GetUser(string user);
}
Ownage
  • 45
  • 4
  • Your endpoint is not correct. it should be /users/:username see https://developer.github.com/v3/users/#get-a-single-user – DOMZE Mar 07 '18 at 21:40
  • It doesn't seem like that worked. I am getting the same issue. – Ownage Mar 07 '18 at 22:27
  • My bad it is indeed /users/... what are you getting as a response/exception? – DOMZE Mar 08 '18 at 01:14
  • I am not getting a response/exception. Code runs successfully but everytime I try to print octocat.login after the getUser method I don't get any output on the console. – Ownage Mar 08 '18 at 01:41
  • you just need to sed UserAgent to HttpClient if you are using Github Api – Naushad Warsi Dec 06 '19 at 04:03
  • please take a look at this [article](https://www.linkedin.com/pulse/type-safe-rest-refit-naushad-warsi/) to better understand Refit. – Naushad Warsi Jul 09 '20 at 08:11

2 Answers2

3

If you want to query GitHub API you need to set a User-Agent

There's 2 way to do this with Refit:

1 - Set the headers on your contract

[Headers("User-Agent: My Favorite User Agent!")]
public interface IGitHubApi
{
    [Get("/users/{user}")]
    Task<User> GetUser(string user);
}

2 - Use a HttpClient and set it there

var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("User-Agent", "My Favorite User Agent!");
httpClient.BaseAddress = new Uri("https://api.github.com");
var gitHubApi = RestService.For<IGitHubApi>(httpClient);
var user = await gitHubApi.GetUser("octocat");
DOMZE
  • 1,369
  • 10
  • 27
0

If you are using the Github API than You need to set the UserAgent in the Header, just as follows,

            {
                BaseAddress = new Uri("https://api.github.com"),
                DefaultRequestHeaders = {UserAgent = { ProductInfoHeaderValue.Parse("NakWarsi")}}    //here I have put my username("NakWarsi") what you need to replace with your username 
            };
            _restApiService = RestService.For<IGitHubApi>(_client);

Here I have created a sample project using Github take a look for detailed information