Perform HTTP Calls using Java 11 HttpClient - Part 2

Making POST call using Java 11 HTTP Client - Java Dev ビット

Perform HTTP Calls using Java 11 HttpClient - Part 2

In this article, we are going to learn how to create a resource. To create a new resource HTTP POST verb is used, unlike the GET request we can send a request body with the POST request. The request body contains the data for creating the resource in the system. We learned how to get started with Java 11 HttpClient to make a GET request. You can check the article here:

Perform HTTP Calls using Java 11 HttpClient - Part 1

Step 1 : Initializing the HttpClient

import java.net.http.HttpClient;
final HttpClient client = HttpClient.newBuilder().build();

We can initialize the HttpClient using the newBuilder static method, this returns a new client instance.

Step 2: Preparing the POST Request and Request Body

import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
final String url = "https://jsonplaceholder.typicode.com/posts"; // (1)
final String body = "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":1}";// (2)

final HttpRequest request = 
                HttpRequest.newBuilder(URI.create(url)) // (3)
                .POST(BodyPublishers.ofByteArray(body.getBytes())) // (4)
                .build();
  • Line 1 initializes the URL string.
  • Line 2 denotes Request payload in form of JSON string.
  • Line 3 uses the newBuilder static method for creating a new request.
  • Line 4 POST method is transforming the request into a POST request. It requires an argument of the BodyPublishers datatype. BodyPublishers class exposes many methods to create an instance from different data types, here we have used source as Byte Array.

Step 3: Executing the Request

import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
final HttpResponse<String> response = 
                client.send(request, BodyHandlers.ofString());

Now, let's execute the POST request by using send provided by the client object created earlier. The response object exposes many functions to get the details of the response from the external service.

Response Methods

System.out.println(response.statusCode());
System.out.println(response.body());

Response of above commands:

201
{
  "id": 101
}

We have successfully made a GET call in 3 simple steps. That's it for this Dev ビット.

Bonus

In Step 2 we have a variable denoting the JSON string, but when we are working we want to create a request payload using Java Objects. These java objects can be converted to a JSON string using Object Serializers/ Deserializers like Jackson.

import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper mapper =  new ObjectMapper();
mapper.writeValueAsString(<object_variable_name>);

The writeValueAsString method is used to convert Java Object to String datatype.

Previous Articles

Feel free to reach out on my Twitter. Check out my other projects on my Github.

Blog_Footer_small_badge.png