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:

Merging Two Maps with Java 8
Write/Read cookies using HTTP and Read a file from the internet
Guide to Selenium with JUnit / TestNG
Map to String Conversion in Java
Java – String to Reader
Introduction to the Java NIO2 File API
A Guide to JPA with Spring
Java Program to Solve TSP Using Minimum Spanning Trees
Spring Security OAuth Login with WebFlux
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
Understanding Memory Leaks in Java
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Java Program to Implement Find all Forward Edges in a Graph
Spring Data JPA @Query
Configuring a DataSource Programmatically in Spring Boot
Java Program to Implement AVL Tree
Java Program to Implement Find all Back Edges in a Graph
Guide to Guava Table
Java Program to Generate Random Hexadecimal Byte
A Guide to @RepeatedTest in Junit 5
Queue và PriorityQueue trong Java
Performance Difference Between save() and saveAll() in Spring Data
Java Program to Convert a Decimal Number to Binary Number using Stacks
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Java Program to Evaluate an Expression using Stacks
Login For a Spring Web App – Error Handling and Localization
Java Program to Implement Selection Sort
Java Program to Implement Quick sort
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
How to use the Spring FactoryBean?
An Introduction to ThreadLocal in Java