Coder Perfect

When using httpClient.GetAsync, you can add headers.


In a Windows Store app project, I’m using to implement an API created by other coworkers.

They demonstrate an example of a procedure I must implement:

var baseAddress = new Uri("");

using (var httpClient = new HttpClient{ BaseAddress = baseAddress })
    using (var response = await httpClient.GetAsync("user/list{?organizationId}"))
        string responseData = await response.Content.ReadAsStringAsync();

I need a header with a token that I got before in this and some other techniques.

Here’s a screenshot of Postman (chrome extension) with the relevant header:

How do I include the Authorization header in my document?

Asked by Thought

Solution #1

This is a later response, but it’s because no one else has given this solution…

You might set headers per request if you don’t want to put the header on the HttpClient instance by adding it to the DefaultRequestHeaders.

However, you’ll have to utilize the SendAsync() method.

If you wish to reuse the HttpClient — which is a good practice — this is the proper solution.

Use it in the following way:

using (var requestMessage =
            new HttpRequestMessage(HttpMethod.Get, ""))
    requestMessage.Headers.Authorization =
        new AuthenticationHeaderValue("Bearer", your_token);

    await httpClient.SendAsync(requestMessage);

Answered by Philippe

Solution #2

When using GetAsync with the HttpClient, the authorisation headers can be included as follows:

                         = new AuthenticationHeaderValue("Bearer", "Your Oauth token");

This adds the authorization header for the life of the HttpClient, which is beneficial if you’re only visiting one site with a static authorisation header.

Here’s a more in-depth SO answer:

Answered by kmcnamee

Solution #3

The accepted answer works, but when I wanted to test adding Accept headers, things got a little more complex. This is what I came up with in the end. It seems simpler to me so I think I’ll stick with it in the future:

client.DefaultRequestHeaders.Add("Accept", "application/*+xml;version=5.1");
client.DefaultRequestHeaders.Add("Authorization", "Basic " + authstring);

Answered by sirdank

Solution #4

This code is sometimes all you need.

 httpClient.DefaultRequestHeaders.Add("token", token);

Answered by Jackdon Wang

Solution #5

You can utilize “Extensions” like this in response to greenhoorn’s answer:

  public static class HttpClientExtensions
        public static HttpClient AddTokenToHeader(this HttpClient cl, string token)
            //int timeoutSec = 90;
            //cl.Timeout = new TimeSpan(0, 0, timeoutSec);
            string contentType = "application/json";
            cl.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType));
            cl.DefaultRequestHeaders.Add("Authorization", String.Format("Bearer {0}", token));
            var userAgent = "d-fens HttpClient";
            cl.DefaultRequestHeaders.Add("User-Agent", userAgent);
            return cl;

And use:

string _tokenUpdated = "TOKEN";
HttpClient _client;

Answered by RDyego

Post is based on