The HttpRequest class encapsulates the information required to be sent across the network to the server by the client. It includes the URI to connect with, headers with a set of variable names and their corresponding values, the timeout value (the time to wait before discarding the request), and the HTTP method to invoke (PUT, POST, GET, or DELETE).
Unlike the HttpClient class, HttpRequest doesn't give you a class instance with the default values, and it makes sense not to. Imagine the URI that the client would connect to if you don't specify it.
Let's create an HttpRequest instance by calling its newBuilder() method and passing a URI to it:
HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("http://www.eJavaGuru.com/")) .build();
You can add the timeout to your requests by using the timeout() method, as follows:
HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("http://www.eJavaGuru.com/")) .timeout(Duration.ofSeconds(240))
.build();
A request instance must include the HTTP method to use. If no method is specified, a GET request is made, by default. In the preceding code, a GET request is made. Let's specify the HTTP method explicitly. The most common HTTP methods are GET and POST. The DELETE and PUT HTTP methods are also used.
The following example specifies the method as the POST method:
HttpRequest request = HttpRequest.newBuilder() .uri(new URI("http://www.eJavaGuru.com/")) .timeout(Duration.ofSeconds(240)) .POST(HttpRequest.noBody()) .build();
The POST method requires you to pass an instance of the BodyProcessor class. For a POST request that doesn't require a body, you can pass HttpRequest.noBody(). You can use multiple sources, such as a string, InputStream, byte array, or file, and pass it to the POST method. Here's an example that passes a file to the POST method:
HttpRequest request = HttpRequest.newBuilder() .uri(new URI("http://www.eJavaGuru.com/")) .timeout(Duration.ofSeconds(240)) .POST(HttpRequest.BodyProcessor .fromFile(Paths.get("data.txt"))) .build();
The following example passes a string to the POST() method:
HttpRequest request = HttpRequest.newBuilder() .uri(new URI("http://www.eJavaGuru.com/")) .timeout(Duration.ofSeconds(240))
.POST(HttpRequest.BodyProcessor .fromString("This is sample data")) .build();
Imagine that you are working with an application that deals with buying shares when their prices rise or fall above or below a threshold. Here's some good news for you. BodyProcessor is a Reactive Stream publisher; you can deal with real-time data (such as stock prices) with controlled back pressure by using it.
Another frequently used method is header(), which specifies the contents of the request. Here's an example, which specifies the contents of request as text/plain:
HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("http://www.eJavaGuru.com/")) .header("Content-Type", "text/plain")
.build();
Here's a list of the important methods of the HttpClient class :
Method Return Type |
Method Name |
Method Description |
abstract Optional<HttpRequest.BodyPublisher> |
bodyPublisher() |
Returns Optional containing the HttpRequest.BodyPublisher instance set on this request |
abstract boolean |
expectContinue() |
Returns the requests to continue setting |
abstract HttpHeaders |
headers() |
The (user-accessible) request headers that this request was (or will be) sent with |
abstract String |
method() |
Returns the request method for this request |
static HttpRequest.Builder |
newBuilder() |
Creates an HttpRequest builder |
static HttpRequest.Builder |
newBuilder (URI uri) |
Creates an HttpRequest builder with the given URI |
abstract Optional<Duration> |
timeout() |
Returns Optional containing this request's timeout duration |
abstract URI |
uri() |
Returns this request's URI |
abstract Optional<HttpClient.Version> |
version() |
Returns Optional containing the HTTP protocol version that will be requested for this HttpRequest |
Unlike the HttpClient and HttpRequest classes, you don't create instances of the HttpResponse class. Let's look at how you can instantiate it in the next section.