DELETE Request using Java 11 HttpClient API

In this article, we are going to see how to perform a DELETE call with new Java 11 HttpClient API.  Let’s learn “How to send a DELETE request using Java 11 HttpClient API?”.

Delete_HttpClient_Request_Java11_Featured_Image_Techndeck

DELETE Request using Java 11 HttpClient API...!!! Click To Tweet

In this tutorial, we are going to cover below topics:

  1. What is HTTP DELETE Request?
  2. How to send DELETE request using HttpClient?

 

Check out: DELETE REQUEST using another popular API testing Framework – REST ASSURED

1. What is HTTP DELETE Request?

DELETE method is quite easy and straightforward to understand as it’s name suggest, it is used to ‘Delete’ any resource specified by a URI.

Some key points of DELETE requests:

  • DELETE 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. Therefore, sending a ‘DELETE’ request again and again on the same resource would end up in the same result as resource is already gone. 
  • But, because of above point, calling ‘Delete’ second time on the same resource would result in 404 (Not Found) status code since it was already gone. This, actually makes DELETE no longer idempotent.
  • ‘200’ (OK) status codes will be returned if the resource gets successfully deleted and the response message representation stating the status while ‘204’ (No Content) could be returned if the action has been enacted but the response body is empty.

 

2. How to send DELETE request 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 DELETE endpoint available at the above-mentioned website which is ‘/delete/{id}’. The full-service URL with endpoint is ‘http://dummy.restapiexample.com/api/v1/delete/{id}’.

At the above resource URL, we are going to make a delete call to remove an existing employee who is having ‘id’ as ‘11400’.

 

NOTE: This ‘id’ belongs to the employee which is generated during the POST call to create the employee. So, this id : 11400 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 DELETE Request Test in place where I am using ‘11400’.

Here is the code to send the DELETE request to the above mentioned REST API Service Endpoint:

 

Let’s try to understand the code:

1. Specify the URL

 

2. Create a Delete Request using HttpRequest builder and pass the resource URI to it

 

3. Create a new HttpClient object

 

4. Submit the DELETE Request with BodyHandler which defines the response body should be of string format, and store the output in the response object

 

5. Finally, extract the status code and response body using the response object created above

 

Output:

That’s it, it’s that simple to make a DELETE Requests using Java 11 HttpClient library.

 

DELETE Request using Java 11 HttpClient API...!!! Click To Tweet

If you like this post , please check out my other useful blog posts on Rest Assured:

Other Useful References:

Leave a Reply

Your email address will not be published.Required fields are marked *

Facebook Twitter Instagram YouTube