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 end-of-day today:

>> GET ACCESS NOW

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

>> GET ACCESS NOW

1. Overview

Java 11 officially introduced the Java HttpClient. Before that, we often used third-party libraries like Apache HttpClient when we needed to use an HTTP client.

In this short tutorial, we'll see how to add custom HTTP headers with the Java HttpClient.

2. Customize HTTP Headers

We can easily add custom headers using one of three methods from the HttpRequest.Builder object: header, headers, or setHeader. Let's see them in action.

2.1. Use header() Method

The header() method allows us to add one header at a time.

We can add the same header name as many times as we want, like in the example below, and they will all be sent:

HttpClient httpClient = HttpClient.newHttpClient();

HttpRequest request = HttpRequest.newBuilder()
  .header("X-Our-Header-1", "value1")
  .header("X-Our-Header-1", "value2")
  .header("X-Our-Header-2", "value2")
  .uri(new URI(url)).build();

return httpClient.send(request, HttpResponse.BodyHandlers.ofString());

2.2. Use headers() Method

If we want to add multiple headers at the same time, we can use the headers() method:

HttpRequest request = HttpRequest.newBuilder()
  .headers("X-Our-Header-1", "value1", "X-Our-Header-2", "value2")
  .uri(new URI(url)).build();

This method also allows us to add multiple values to one header name:

HttpRequest request = HttpRequest.newBuilder()
  .headers("X-Our-Header-1", "value1", "X-Our-Header-1", "value2")
  .uri(new URI(url)).build();

2.3. Use setHeader() Method

Finally, we can use the setHeader() method to add a header. But, unlike the header() method, if we use the same header name more than once, it will overwrite any previous header(s) that we had set with that name:

HttpRequest request = HttpRequest.newBuilder()
  .setHeader("X-Our-Header-1", "value1")
  .setHeader("X-Our-Header-1", "value2")
  .uri(new URI(url)).build();

In the example above, the value of our header will be “value2”.

3. Conclusion

In summary, we've learned different ways to add custom HTTP headers with the Java HttpClient.

As always, the example code from this article is available over on GitHub.

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

>> GET ACCESS NOW

HTTPClient footer
1 Comment
Oldest
Newest
Inline Feedbacks
View all comments
Comments are closed on this article!