methods newBuilder(), build()

The HttpRequest class is used to describe an http-request, which is easy to understand from its name. This object does nothing by itself, it just contains various information about the http request. Therefore, as you probably already guess, the Builder template is also used to create it.

HttpRequest request = HttpRequest.newBuilder()
	.method1()
	.method2()
	.methodN()
    .build();

Where between calls to the methods newBuilder() and build() you need to call all the methods to construct an objectHttpRequest.

An example of a simple request looks like this:

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create(“http://codegym.cc”))
    .build();
HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString());

You can find all methods of the HttpRequest class at the link in the official documentation .

And then we will analyze the most popular of them.

uri() Method

Using the uri() method , you can set the URI (or URL) to which the http request will be sent. Example:

HttpRequest request = HttpRequest.newBuilder()
    .uri( URI.create(“http://codegym.cc”) )
    .build();

By the way, you can make this code a little shorter by passing the URI directly to the newBuilder() method :

HttpRequest request = HttpRequest.newBuilder( URI.create(“http://codegym.cc”) ).build();

Important! A URI can be created in two ways:

  • newURI(String)
  • URI.create(String)

The second way is preferable. The first way, unfortunately, is not very good, because the URI constructor is declared as public URI(String str) throws URISyntaxException , and URISyntaxException is a checked exception.

Methods GET(), POST(), PUT(), DELETE()

You can set the http request method using the following methods:

  • GET()
  • POST()
  • PUT()
  • DELETE()

Here's what a simple GET request would look like:

HttpRequest request = HttpRequest.newBuilder()
  .uri(new URI("https://codegym.cc"))
  .GET()
  .build();

version() method

You can also set the HTTP protocol version. There are only 2 options:

  • HttpClient.Version.HTTP_1_1
  • HttpClient.Version.HTTP_2

Let's say you want to create a request using the HTTP/2 protocol, then you will need to write:

HttpRequest request = HttpRequest.newBuilder()
   .uri(new URI("https://codegym.cc"))
   .version( HttpClient.Version.HTTP_2 )
   .GET()
   .build();

Very simple, isn't it? :)

timeout() method

You can also set the query execution time. If it passes and the request is never completed, an HttpTimeoutException will be thrown .

The time itself is set using the objectdurationfrom Java DateTime API. Example:

HttpRequest request = HttpRequest.newBuilder()
   .uri(new URI("https://codegym.cc"))
   .timeout( Duration.of(5, SECONDS) )
   .GET()
   .build();

The presence of this method shows that the HttpClient and HttpRequest classes can perform a variety of tasks. Imagine that you are executing a request, and something happened to the network and it lasted 30 seconds. It is much more useful to immediately receive an exception and react to it correctly.

header() method

You can also add any number of headers to any request. And it's as easy to do as anything else. There is a special method for this - header() . Example:

HttpRequest request = HttpRequest.newBuilder()
   .uri(new URI("https://codegym.cc"))
   .header("name1", "value1")
   .header("name2", "value2")
   .GET()
   .build();

There is another alternative way to set many headers at once. It may come in handy if, for example, you converted a list of headers into an array:

HttpRequest request = HttpRequest.newBuilder()
   .uri(new URI("https://codegym.cc"))
   .headers("name1", "value1", "name2", "value2")
   .GET()
   .build();

Everything elementary is simple.