Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

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 Apache 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()
  .setUri(SAMPLE_URL)
  .setHeader(HttpHeaders.CONTENT_TYPE, "application/json")
  .build();
client.execute(request);

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");
client.execute(request);

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();
client.execute(request);

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.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

HTTPClient footer
Comments are closed on this article!