Yesterday’s post, “Tip - How To Correctly Add Response HttpHeaders In C# & .NET”, looked at how to add headers to HTTP Responses by accessing and modifying the Headers collection of the Response object of the HttpContext.

Today’s post will look at how to do the same from the client, i.e. the Request.

Here we have to decide the following:

  1. Will the header be sent with all requests?
  2. Is the header specific to particular request?

All Requests

If the header will be sent for all requests, you would typically access the DefaultRequestHeaders property of the HttpClient and set the header (and value) that you want.

var client = new HttpClient();
// Set the default headers	
client.DefaultRequestHeaders.Add("YourHeaderNameHere", "YourValueHere");

Per Request

If, for some reason, your header is only valid for a particular request, you have to do a bit more heavy lifting.

The code will be as follows:

// Create a request
var request = new HttpRequestMessage(HttpMethod.Get, "https://example.com");
// Add your header and value
request.Headers.Add("YourHeaderNameHere", "YourValueHere");
// Send the request and await the response
var response = await client.SendAsync(request);

The logic here is as follows:

  1. Create a HttpRequestMessage (can be a GET, POST, etc)
  2. Add your header
  3. Send the HttpRequestMessage
  4. Await the response

Reminder: the HttpClient will set the casing for the headers for you.

responseHedersCasing

This generally won’t be an issue, but there are cases where the downstream server expects the header to be of a particular case.

In such a situation you can use the method outlined in the post “Controlling The Casing Of Submitted Request Headers In C# & .NET

TLDR

You can configure a HttpClient to send specific headers for all requests, or you can configure the headers to be specific to requests.

Happy hacking!