Skip to content

Instantly share code, notes, and snippets.

@tdshipley
Last active March 10, 2023 16:17
Show Gist options
  • Save tdshipley/869630ea14abd246310f to your computer and use it in GitHub Desktop.
Save tdshipley/869630ea14abd246310f to your computer and use it in GitHub Desktop.
An example of GET request using HttpClient in C#
namespace API.Controllers
{
public class GithubController : ApiController
{
private const string _address = "https://api.github.com/users/tdshipley";
private const string _userAgent = "TestApp";
// GET api/<controller>
public async Task<string> Get()
{
var result = await GetAsync(_address);
return result.ToString();
}
private async Task<JObject> GetAsync(string uri)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", "token ADD YOUR OAUTH TOKEN");
client.DefaultRequestHeaders.Add("User-Agent", _userAgent);
var content = await client.GetStringAsync(uri);
return JObject.Parse(content);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment