Ric Ric - 12 days ago 5
C# Question

Adding headers when using httpClient.GetAsync

Im implementing a api made by other collegues with Apiary.io, in a windows store app project.

they show this example of a method i have to implement

var baseAddress = new Uri("https://private-a8014-xxxxxx.apiary-mock.com/");

using (var httpClient = new HttpClient{ BaseAddress = baseAddress })
{

using(var response = await httpClient.GetAsync("user/list{?organizationId}"))
{


string responseData = await response.Content.ReadAsStringAsync();

}
}


in this and some other methods i need to have a header with a token that i get before

heres a image of postman( chrome extension ) with the header im talking about
enter image description here

how do i add that Authorization header to the request?

Answer

You add them to httpclient like so:

httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "Your Oauth token");

Here is an detailed SO answer

Comments