In this tutorial, we are going to see “How to send a DELETE request using Apache HttpClient by utilizing HttpDelete method?”.
Simple representation of DELETE Request using Apache HttpClient in Java...!!! Share on X
In this tutorial, we are going to cover below topics:
- What is HTTP DELETE Request?
- How to send DELETE request using Apache 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 Apache 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:
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 43 44 45 46 47 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpDelete; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.testng.annotations.Test; /** * This class shows how to send a DELETE Request using 'HttpDelete' method of Apache HttpClient library. * @author Deepak Verma */ public class Delete_Request_Apache_HttpClient { @Test public void deleteEmployee() throws ClientProtocolException, IOException { String deleteEndpoint = "http://dummy.restapiexample.com/api/v1/delete/11400"; CloseableHttpClient httpclient = HttpClients.createDefault(); HttpDelete httpDelete = new HttpDelete(deleteEndpoint); System.out.println("Executing request " + httpDelete.getRequestLine()); HttpResponse response = httpclient.execute(httpDelete); BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); //Throw runtime exception if status code isn't 200 if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } //Create the StringBuffer object and store the response into it. StringBuffer result = new StringBuffer(); String line = ""; while ((line = br.readLine()) != null) { System.out.println("Response : \n"+result.append(line)); } } } |
Let’s try to understand the code:
1. Specify the URL and set up CloseableHttpClient object
1 2 3 | String deleteEndpoint = "http://dummy.restapiexample.com/api/v1/delete/11400"; CloseableHttpClient httpclient = HttpClients.createDefault(); |
2. Create a basic Delete Request and pass the resource URI to it
1 | HttpDelete httpDelete = new HttpDelete(deleteEndpoint); |
3. Submit the Request using HttpDelete -> Execute method
1 | HttpResponse response = httpclient.execute(httpDelete); |
4. Create a BufferedReader object and store the raw Response content into it.
1 | BufferedReader br = new BufferedReader( new InputStreamReader((response.<wbr />getEntity().getContent()))); |
5. Throw runtime exception if status code isn’t 200
1 2 3 | if (response.getStatusLine().<wbr />getStatusCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().<wbr />getStatusCode()); } |
6. Create the StringBuffer object and store the response into it.
1 2 3 4 5 6 7 | StringBuffer result = new StringBuffer(); String line = ""; while ((line = br.readLine()) != null) { System.out.println("Response : \n"+result.append(line)); } |
Eclipse Console Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [RemoteTestNG] detected TestNG version 6.14.3 Executing request DELETE http://dummy.restapiexample.com/api/v1/delete/11400 HTTP/1.1 Response : {"success":{"text":"successfully! deleted Records"}} PASSED: createEmployee =============================================== 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 DELETE Request using Apache HttpClient in Java: ?
Simple representation of DELETE Request using Apache HttpClient...!!! Share on XIf you like this post , please check out my other useful blog posts
- How to send a POST Request using Apache HttpClient
- How to make a GET Request using Apache HttpClient
- How to make a PUT Request using Apache HttpClient
Other Useful References: