Skip to content

Instantly share code, notes, and snippets.

@GeoffCox
Last active December 10, 2020 23:24
Show Gist options
  • Save GeoffCox/c0ba0a842bbe22c9215e to your computer and use it in GitHub Desktop.
Save GeoffCox/c0ba0a842bbe22c9215e to your computer and use it in GitHub Desktop.
Call GitHub REST API using HttpClient
WebRequestHandler handler = new WebRequestHandler()
{
UseProxy = true,
};
var client = new HttpClient(handler);
client.BaseAddress = new Uri("https://api.github.com/");
// You should set the version so that GitHub knows what API you area calling
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
// You must set a user agent so that the CRLF requirement on the header parsing is met.
// Otherwise you will get an excpetion message with "The server committed a protocol violation. Section=ResponseStatusLine"
client.DefaultRequestHeaders.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue("Mozilla", "5.0"));
// Set this to the URL you want.
var response = client.GetAsync("users/bellacode/repos").Result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment