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:

Guide to Java 8’s Collectors
Java Program to Compute Determinant of a Matrix
Java Program to find the peak element of an array using Binary Search approach
How to Manually Authenticate User with Spring Security
Java Program to Represent Graph Using Incidence List
Java Program to Implement Stack
Java Program to Implement Gale Shapley Algorithm
wait() and notify() Methods in Java
Spring Cloud Bus
Java Program to Perform Matrix Multiplication
Logging in Spring Boot
So sánh HashSet, LinkedHashSet và TreeSet trong Java
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Bootstrapping Hibernate 5 with Spring
Convert Character Array to String in Java
Reactive WebSockets with Spring 5
Simplify the DAO with Spring and Java Generics
Java Program to Solve Knapsack Problem Using Dynamic Programming
Guide to the Synchronized Keyword in Java
String Processing with Apache Commons Lang 3
Java Program to Implement Stein GCD Algorithm
Spring Boot - Code Structure
Finding the Differences Between Two Lists in Java
Receive email by java client
Java Program to Implement Dijkstra’s Algorithm using Set
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
Introduction to the Java ArrayDeque
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Request a Delivery / Read Receipt in Javamail
Java Program to Implement Counting Sort
Java Stream Filter with Lambda Expression