Adding Parameters to HttpClient Requests

1. Introduction

HttpClient is part of the Apache HttpComponents project that provides a toolset of low-level Java components focused on HTTP and associated protocols. The most essential function of HttpClient is to execute HTTP methods.

In this short tutorial, we’ll discuss adding parameters to HttpClient requests. We’ll learn how to use UriBuilder with String name-value pairs and also NameValuePairs. Similarly, we’ll see how to pass parameters using UrlEncodedFormEntity.

2. Add Parameters to HttpClient Requests Using UriBuilder

UriBuilder helps us to easily create URIs and add parameters via builder pattern. We can add parameters using String name-value pairs, or utilize NameValuePairs class for that purpose.

In this example, a final URL should look like this:

https://example.com?param1=value1¶m2=value2

Let’s see how to use String name-value pairs:

public CloseableHttpResponse sendHttpRequest() {
    HttpGet httpGet = new HttpGet("https://example.com");
    URI uri = new URIBuilder(httpGet.getURI())
      .addParameter("param1", "value1")
      .addParameter("param2", "value2")
      .build();
   ((HttpRequestBase) httpGet).setURI(uri);
    CloseableHttpResponse response = client.execute(httpGet);
    client.close();
}

Also, we can go with the NameValuePair list for HttpClient request:

public CloseableHttpResponse sendHttpRequest() {
    List nameValuePairs = new ArrayList();
    nameValuePairs.add(new BasicNameValuePair("param1", "value1"));
    nameValuePairs.add(new BasicNameValuePair("param2", "value2"));
    HttpGet httpGet = new HttpGet("https://example.com");
    URI uri = new URIBuilder(httpGet.getURI())
      .addParameters(nameValuePairs)
      .build();
   ((HttpRequestBase) httpGet).setURI(uri);
    CloseableHttpResponse response = client.execute(httpGet);
    client.close();
}

Similarly, UriBuilder can be used to add parameters to other HttpClient request methods.

3. Add Parameters to HttpClient Request Using UrlEncodedFormEntity

Another approach would be to utilize UrlEncodedFormEntity:

public CloseableHttpResponse sendHttpRequest() {
    List nameValuePairs = new ArrayList();
    nameValuePairs.add(new BasicNameValuePair("param1", "value1"));
    nameValuePairs.add(new BasicNameValuePair("param2", "value2"));
    HttpPost httpPost = new HttpPost("https://example.com");
    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));
    CloseableHttpResponse response = client.execute(httpPost);
    client.close();
}

Notice that UrlEncodedFormEntity couldn’t be used for GET requests, since GET request does not have a body that could contain an entity.

4. Conclusion

In this example, we showed how to add parameters to HttpClient requests. Also, the implementation of all these examples and code snippets are available over on GitHub.

Related posts:

LinkedList trong java
Spring Boot - Sending Email
Java Program to Check whether Graph is a Bipartite using BFS
Autoboxing và Unboxing trong Java
The Spring @Controller and @RestController Annotations
Spring Security OAuth2 – Simple Token Revocation
Java Program to implement Bi Directional Map
Vector trong Java
Overflow and Underflow in Java
Java Program to Implement Hash Tables Chaining with List Heads
Tính trừu tượng (Abstraction) trong Java
Performance Difference Between save() and saveAll() in Spring Data
Using the Not Operator in If Conditions in Java
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Spring Cloud – Tracing Services with Zipkin
Receive email by java client
Java Program to Implement First Fit Decreasing for 1-D Objects and M Bins
Debugging Reactive Streams in Java
Introduction to Spring Boot CLI
Getting Started with Custom Deserialization in Jackson
Java Program to Implement Meldable Heap
Java Program to Implement Binary Search Tree
Java Program to Check Cycle in a Graph using Topological Sort
Custom Error Pages with Spring MVC
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree
Getting the Size of an Iterable in Java
Java Program to Implement Segment Tree
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
Java Program to Implement Leftist Heap
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
Java Program to Perform the Shaker Sort