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 5 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&param2=value2

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

HttpGet httpGet = new HttpGet("https://postman-echo.com/get");
URI uri = new URIBuilder(httpGet.getUri()).addParameter("param1", "value1")
   .addParameter("param2", "value2")
   .build();
httpGet.setUri(uri);

try (CloseableHttpClient client = HttpClients.createDefault();
   CloseableHttpResponse response = (CloseableHttpResponse) client
     .execute(httpGet, new CustomHttpClientResponseHandler())) {

   final int statusCode = response.getCode();
   assertThat(statusCode, equalTo(HttpStatus.SC_OK));
}

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

@BeforeEach
 public void setUp() {
    nameValuePairs = new ArrayList<>();
    NameValuePair param1NameValuePair = new BasicNameValuePair("param1", "value1");
    NameValuePair param2NameValuePair = new BasicNameValuePair("param2", "value2");
    nameValuePairs.add(param1NameValuePair);
    nameValuePairs.add(param2NameValuePair);
}       


HttpGet httpGet = new HttpGet("https://postman-echo.com/get");
URI uri = new URIBuilder(httpGet.getUri()).addParameters(nameValuePairs)
      .build();
httpGet.setUri(uri);

try (CloseableHttpClient client = HttpClients.createDefault();
   CloseableHttpResponse response = (CloseableHttpResponse) client
       .execute(httpGet, new CustomHttpClientResponseHandler())) {

    final int statusCode = response.getCode();
    assertThat(statusCode, equalTo(HttpStatus.SC_OK));
}

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:

HttpPost httpPost = new HttpPost("https://postman-echo.com/post");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));

 try (CloseableHttpClient client = HttpClients.createDefault();
    CloseableHttpResponse response = (CloseableHttpResponse) client
        .execute(httpPost, new CustomHttpClientResponseHandler())) {

     final int statusCode = response.getCode();
     assertThat(statusCode, equalTo(HttpStatus.SC_OK));
}

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.