So far, we have covered sending a GET & POST Request in our tutorial on Java 11 HttpClient API. If you haven’t checked that, lets check ‘Sending GET Request’ by clicking this link and also check ‘Sending POST request’ using this link. Now, in this example, we are going to see “How to send a PUT request with JSON as request body using Java 11 HttpClient API?”.
PUT Request using Java 11 HttpClient API...!!! Share on XIn this tutorial, we are going to cover below topics:
- What is HTTP PUT Request?
- How to send PUT request with JSON using HttpClient?
Check out: PUT REQUEST using another popular API testing Framework – REST ASSURED
Let’s begin:
1. What is HTTP PUT Request?
PUT method requests for the enclosed entity are stored under the Request-URI. An update operation will happen if the Request-URI refers to already existing resource otherwise there will be a create operation takes place if Request-URI is a valid resource URI.
Some key points of PUT requests:
- PUT is idempotent means if you try to make a request multiple times, it would result in the same output as it would have no effect.
- PUT should be used when you want to modify a resource which is already a part of resource collection as PUT method would replace the resource entirely.
- PUT resource can be cached.
2. How to send PUT request with JSON using Java 11 HttpClient?
In this tutorial, we will test the ‘Dummy Sample Rest API’ which is available here. This page contains Fake Online REST API for the testing purposes which are performing various CRUD operations.
Let’s take an example of one of the API PUT endpoint available at the above-mentioned website which is ‘/update/{id}’. The full-service URL with endpoint is ‘http://dummy.restapiexample.com/api/v1/update/{id}’.
At the above resource URL, we are going to submit data in the form of JSON to update an existing employee which is having ‘id’ as ‘4710’.
NOTE: This ‘id’ belongs to the employee which is generated during the POST call to create the employee. So, this id : 4710 generated for me when I had executed the create employee using the POST call. In order to get yours, first execute the POST Request call available at this link and grab the ‘id‘ from the response and use it in your PUT Request Test below in place where I am using ‘4710’.
JSON Request Body:
1 2 3 4 5 | { "name":"put_test_employee", "salary":"1123", "age":"23" } |
Here is the code to send the PUT request (containing request body in JSON format) to the above mentioned REST API Service Endpoint:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import java.io.IOException; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import org.testng.annotations.Test; /** * This class shows how to send a PUT Request with JSON using Java 11 HttpClient library. * @author Deepak Verma **/ public class HttpClient_Put_Sync_Call_Java11 { @Test public void updateEmployee() throws IOException, InterruptedException { String putEndpoint = "http://dummy.restapiexample.com/api/v1/update/4710"; String inputJson = "{\n" + " \"name\": \"put_test_employee\",\n" + " \"salary\": \"1123\",\n" + " \"age\": \"23\"\n" + "}"; var request = HttpRequest.newBuilder() .uri(URI.create(putEndpoint)) .header("Content-Type", "application/json") .PUT(HttpRequest.BodyPublishers.ofString(inputJson)) .build(); var client = HttpClient.newHttpClient(); var response = client.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.statusCode()); System.out.println(response.body()); } } |
Let’s try to understand the code:
1. Specify the URL
1 | String putEndpoint = "http://dummy.restapiexample.com/api/v1/update/4710"; |
2. Create a PUT Request using HttpRequest builder that takes JSON as input and pass the resource URI to it
1 2 3 4 5 | var request = HttpRequest.newBuilder() .uri(URI.create(putEndpoint)) .header("Content-Type", "application/json") .PUT(HttpRequest.BodyPublishers.ofString(inputJson)) .build(); |
3. Create a new HttpClient object
1 | var client = HttpClient.newHttpClient(); |
4. Submit the PUT Request with BodyHandler which defines the response body should be of string format, and store the output in the response object
1 | var response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
5. Finally, extract the status code and response body using the response object created above
1 2 | response.statusCode()); response.body()); |
Eclipse Console Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [RemoteTestNG] detected TestNG version 6.14.3 200 {"name":"put_test_employee","salary":"1123","age":"23"} PASSED: updateEmployee =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 1, Failures: 0, Skips: 0 =============================================== |
That’s it, it’s that simple to make a PUT Request with JSON body using new Java 11 HttpClient library: ?
PUT Request using Java 11 HttpClient API...!!! Share on X
If you like this post , please check out my other useful blog posts on Rest Assured:
Other Useful References: