0

I am creating a base class, inherited by multiple clients to utilize. They all use different headers.

I have a set of header values I want to pass to my base class with the 'client' methods that set the client.DefaultRequestHeaders variable

AuthenticationHeaderValue x = new AuthenticationHeaderValue(authValue.Parameter);
NameValueHeaderValue y = new NameValueHeaderValue("AMSConnect-Version", "1_0_0");
NameValueHeaderValue y2 = new NameValueHeaderValue("AMSConnect-Environment", "Dev");

Normally, I'd set up something like this if I was only trying to interface with once API, and one set of of headers...

using (HttpClient client = new HttpClient(handler) { BaseAddress = baseAddress })
{
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", this.SttiServiceEnv.AccessToken);
    client.DefaultRequestHeaders.Add("AMSConnect-Version", "1_0_0");
    client.DefaultRequestHeaders.Add("AMSConnect-Environment", "Dev");

However, the type of client.DefaultRequestHeaders (HttpRequestHeaders) has no setters.

How do I create an array of headers and pass them to the client like this:

using (HttpClient client = new HttpClient(handler) { BaseAddress = baseAddress })
{
   client.DefaultRequestHeaders = someVariable;

EDIT: I get it that the Authorization would be a different type, but I'm hoping there is a container for this, and other types of headers.

Divins Mathew
  • 2,908
  • 4
  • 22
  • 34
CarComp
  • 1,929
  • 1
  • 21
  • 47
  • Why does the second block of code not work? Thats how you add to the `DefaultRequestHeaders` property because there are no setters. – maccettura Feb 13 '17 at 17:45
  • I am creating a base class, inherited by multiple clients to utilize. They all use different headers. Added this to the question. – CarComp Feb 13 '17 at 17:46
  • Well that defeats the purpose of the client DefaultHeaders, add the headers to each request instead – maccettura Feb 13 '17 at 17:47
  • Ok, so how do you do it? Show me how to do it correctly. If DefaultHeaders is the wrong way to do it, what should I use? – CarComp Feb 13 '17 at 17:49
  • 1
    Wasn't trying to come off like a jerk, sorry. Posted an answer below. – maccettura Feb 13 '17 at 17:59

1 Answers1

1

The DefaultRequestHeaders property is used when you have the same header(s) that are in every request you are making with the HttpClient. This saves you the trouble of repeating code every time you make a request.

You are running into a problem where you have different headers for each request, so instead of adding these headers to the HttpClient, add them to every request:

//HttpRequestMessage takes an HttpMethod (GET, POST, etc) and a url as parameters.
using (var request = new HttpRequestMessage(HttpMethod.Get, "/api/users"))
{
    message.Headers.Add("authorization", "bearer 1231098120938123");
    var response = await HttpClient.SendAsync(request);
}

Also, you should know that wrapping HttpClient in a using statement for each request is poor practice. See this answer for more details

Community
  • 1
  • 1
maccettura
  • 10,514
  • 3
  • 28
  • 35