Last active
March 10, 2023 16:17
-
-
Save tdshipley/869630ea14abd246310f to your computer and use it in GitHub Desktop.
An example of GET request using HttpClient in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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