Custom HTTP Header with the HttpClient

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.

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.

Related posts:

Handling Errors in Spring WebFlux
Java Program to Represent Graph Using Incidence List
Java Program to implement Associate Array
Uploading MultipartFile with Spring RestTemplate
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Use Liquibase to Safely Evolve Your Database Schema
Java Program to Implement Tarjan Algorithm
Java Program to Generate All Subsets of a Given Set in the Gray Code Order
Java Program to Perform the Shaker Sort
Java Program to Implement Euler Circuit Problem
Hướng dẫn Java Design Pattern – Chain of Responsibility
Send an email with an attachment
Java Program to Implement Karatsuba Multiplication Algorithm
Lập trình đa luồng trong Java (Java Multi-threading)
The Spring @Controller and @RestController Annotations
Jackson – Decide What Fields Get Serialized/Deserialized
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
Introduction to Spring Method Security
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Sort a HashMap in Java
Debug a JavaMail Program
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Transaction Propagation and Isolation in Spring @Transactional
Spring Cloud – Tracing Services with Zipkin
A Guide to the ResourceBundle
What is Thread-Safety and How to Achieve it?
Format ZonedDateTime to String
Java Program to Find Transpose of a Graph Matrix
Set Interface trong Java
How to Count Duplicate Elements in Arraylist
Java Program to Check whether Graph is a Bipartite using BFS
Spring Webflux and CORS