Custom header to HttpClient request

Adding a Custom Header to an HttpClient Request
If you're working with HttpClient in your code and need to add a custom header to your request, you're in the right place! In this blog post, we'll go over how to easily add a custom header using the HttpClient's DefaultRequestHeaders property.
The Problem
Let's look at the example code provided:
using (var client = new HttpClient()) {
client.BaseAddress = new Uri("https://api.clickatell.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "xxxxxxxxxxxxxxxxxxxx");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = client.PostAsJsonAsync("rest/message", svm).Result;
}The code snippet above shows an example of using HttpClient to send a POST request with JSON data. However, the custom header "X-Version: 1" needs to be added to the request. How can we accomplish this?
The Solution
To add a custom header to your HttpClient request, you can simply use the Add method on the DefaultRequestHeaders property. Here's how you can modify your code to include the custom header:
using (var client = new HttpClient()) {
client.BaseAddress = new Uri("https://api.clickatell.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "xxxxxxxxxxxxxxxxxxxx");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("X-Version", "1"); // Adding the custom header
var response = client.PostAsJsonAsync("rest/message", svm).Result;
}By using the Add method on DefaultRequestHeaders, we can append our custom header to the existing headers.
Explanation
Let's break down the code, so we understand what's happening:
We create a new instance of the HttpClient class and assign it to the
clientvariable.We set the
BaseAddressproperty of theclientvariable to the desired base address of the API we want to call.We clear the
Acceptheaders to ensure a clean slate for our headers.We set the
Authorizationheader using theAuthenticationHeaderValueclass, which allows us to specify the authentication scheme and token value.We add an
Acceptheader ofapplication/jsonto indicate that we want to receive JSON data in the response.We add the custom header "X-Version" with a value of "1" using the
Addmethod onDefaultRequestHeaders.Finally, we make the POST request using the
PostAsJsonAsyncmethod.
Conclusion
Adding a custom header to an HttpClient request is straightforward and can be accomplished by using the Add method on the DefaultRequestHeaders property. By following the provided example, you'll be able to include any custom headers you need in your HttpClient requests.
Now it's your turn to add that extra layer of customization to your API requests! Try it out, experiment, and see how you can leverage custom headers to enhance your interactions with APIs.
If you have any questions or faced any issues, please let me know in the comments below. I'm here to help!
💪🚀 Happy coding!
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.



