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:

Apache Commons Collections SetUtils
How to Count Duplicate Elements in Arraylist
Java Switch Statement
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Display Auto-Configuration Report in Spring Boot
Reading an HTTP Response Body as a String in Java
Java Program to Implement Interpolation Search Algorithm
HttpClient 4 – Follow Redirects for POST
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Java Byte Array to InputStream
Netflix Archaius with Various Database Configurations
Guide to java.util.concurrent.Locks
A Guide to Concurrent Queues in Java
Java Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
Java Program to Construct an Expression Tree for an Postfix Expression
Sorting Query Results with Spring Data
Getting a File’s Mime Type in Java
Zipping Collections in Java
Java Program to Solve the Fractional Knapsack Problem
Các nguyên lý thiết kế hướng đối tượng – SOLID
Java Program to Check if it is a Sparse Matrix
Spring Boot Change Context Path
Overview of the java.util.concurrent
Java Program to Implement Doubly Linked List
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Java Program to Implement Quick Sort Using Randomization
Java Program to Implement Brent Cycle Algorithm
Hướng dẫn Java Design Pattern – Memento
Java Program to Implement Find all Back Edges in a Graph
Java – Write to File
Java Program to Find Nearest Neighbor Using Linear Search
Immutable ArrayList in Java