Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:

>> GET ACCESS NOW

1. Introduction

In this tutorial, we'll discuss adding parameters to the Java HttpClient requests.

The Java HTTPClient is available as a built-in functionality from Java 11. Therefore, we can send HTTP requests without using third-party libraries like Apache HttpClient and OkHttp.

2. Adding Parameters

HttpRequest.Builder helps us to easily create HTTP requests and add parameters using the builder pattern.

The Java HttpClient API does not provide any methods to add query parameters. Although we could make use of third-party libraries like URIBuilder from Apache HttpClient to build a request URI string. Let´s see what it would be like to use only the functionality added in Java 11:

HttpRequest request = HttpRequest.newBuilder()
  .version(HttpClient.Version.HTTP_2)
  .uri(URI.create("https://postman-echo.com/get?param1=value1¶m2=value2"))
  .GET()
  .build();

Notice that we have set the version() method to use HTTP version 2. The Java HTTPClient uses HTTP 2 by default. However, if servers do not support requests with HTTP 2,  the version will automatically be downgraded to HTTP 1.1.

Also, we have used GET() as the HTTP request method which is the default. If we do not specify the HTTP request method, the default method GET would be used.

Finally, we can also write the same request in a concise form with the default configuration:

HttpRequest request = HttpRequest.newBuilder()
  .uri(URI.create("https://postman-echo.com/get?param1=value1¶m2=value2"))
  .build();

3. Conclusion

In this example, we covered how to add parameters to the Java HTTPClient requests. Also, the implementations of all these examples and code snippets are available over on GitHub.

In the examples, we've used sample REST endpoints provided by https://postman-echo.com.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:

>> GET ACCESS NOW

HTTPClient footer
Comments are closed on this article!