1. Overview

In this tutorial, we'll look at how to set a custom header with the HttpClient.

If you want to dig deeper and learn other cool things you can do with the HttpClient – head on over to the main HttpClient tutorial.

Further reading:

Do a Simple HTTP Request in Java

A quick and practical guide to performing basic HTTP requests using Java's built-in HttpUrlConnection.

Advanced HttpClient Configuration

HttpClient configurations for advanced use cases.

Exploring the New HTTP Client in Java

Explore the new Java HttpClient API which provides a lot of flexibility and powerful features.

2. Set Header on Request – 4.3 and Above

HttpClient 4.3 has introduced a new way of building requests – the RequestBuilder. To set a header, we'll use the setHeader method – on the builder:

HttpClient client = HttpClients.custom().build();
HttpUriRequest request = RequestBuilder.get()
  .setHeader(HttpHeaders.CONTENT_TYPE, "application/json")

3. Set Header on Request – Before 4.3

In versions pre 4.3 of HttpClient, we can set any custom header on a request with a simple setHeader call on the request:

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(SAMPLE_URL);
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");

As we can see, we're setting the Content-Type directly on the request to a custom value – JSON.

4. Set Default Header on the Client

Instead of setting the Header on each and every request, we can also configure it as a default header on the Client itself:

Header header = new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json");
List<Header> headers = Lists.newArrayList(header);
HttpClient client = HttpClients.custom().setDefaultHeaders(headers).build();
HttpUriRequest request = RequestBuilder.get().setUri(SAMPLE_URL).build();

This is extremely helpful when the header needs to be the same for all requests – such as a custom application header.

5. Conclusion

This article illustrated how to add an HTTP header to one or all requests sent via the Apache HttpClient.

The implementation of all these examples and code snippets can be found in the GitHub project.

Generic bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

Newest Most Voted
Inline Feedbacks
View all comments
5 years ago

I tried using method addHeader but I got error saying method addHeader is undefined for the type HttpGet.

Can you please help me how to use method addheader/setHeader?

Eugen Paraschiv
5 years ago
Reply to  Shilpa

Hey Shilpa – I just ran the code using the addHeader call again and everything runs fine. So – if you want me to take a look at your specific usecase, please raise an issue on github with the code sample, or a pull request – and I’d be happy to see what the problem is. Cheers,

5 years ago

Thanks Eugen for your reply. Can you please share your imports? I have my code on github but its internal to my employer. let me try to share the code via public link.

Eugen Paraschiv
5 years ago
Reply to  Shilpa

Hey Shilpa – so, all the code samples for the article (including the exact imports) are in the project I pointed to at the end of the article. Hope they help. Cheers,

5 years ago

Hi Eugen,

Was wondering if you have tried or seen any example or have clue how to use HttpClient within Spring controller to simulate FORM submit using Http.POST.

Use Case:
User fills up basic details on JSP and submit.
Controller fetches associated details based on input from JSP.
Controller calls external URL from within Spring controller and send required parameters as form POST.

JSP –> Spring controller –> HTTP POST on external URL.

I understand Spring’s “redirect:” , “RedirectView” but the problem is they convert request as GET and query parameters gets appended to URL.

Eugen Paraschiv
5 years ago
Reply to  varun

Hey Varum – if I understand your scenario fully, you need to basically proxy the form submission. You have 2 options – first, you could look into a proxy solution that’s not necessarily Spring focused – that might allow you to more easily to the submission. Second, you can, very carefully – simulate the submission request. So, from the Controller, you use the HttpClient and do a request similar to your original submission. It’s a very manual process, with quite a bit of complexity, so I wouldn’t necessarily recommend doing it, but if you need to, it’s technically doable. Best… Read more »

Comments are closed on this article!