In this quick article, we will discuss step by step how to use Apache HttpClient 4.5 to make an HTTP DELETE request. The HTTP DELETE Request Method requests delete the resource specified by the URI.
HttpClient supports out of the box all HTTP methods defined in the HTTP/1.1 specification: GET, HEAD, POST, PUT, DELETE, TRACE, and OPTIONS. There is a specific class for each method type.: HttpGet, HttpHead, HttpPost, HttpPut, HttpDelete, HttpTrace, and HttpOptions.
In this example, we will use HttpDelete class to handle DELETE HTTP method.
Related Apache HttpClient useful articles:
If you want to dig deeper and learn other cool things you can do with the HttpClient – head on over to the main Apache HttpClient tutorial.
Using the Apache HttpClient
The Apache HttpClient library allows handling HTTP requests. To use this library add a dependency to your Maven or Gradle build file. You find the latest version here: https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
We use maven to manage our dependencies and are using Apache HttpClient version 4.5. Add the following dependency to your project in order to make HTTP DELETE request method.
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
Development Steps
Let's create a step by step example to make an Http DELETE request using HttpClient.
Create instance of CloseableHttpClient using helper class HttpClients.
CloseableHttpClient httpclient = HttpClients.createDefault()
The HttpClients.createDefault() method creates CloseableHttpClient instance with default configuration.
Create basic DELETE request
HttpDelete httpDelete = new HttpDelete("http://localhost:8080/api/v1/users/5");
Create a custom response handler
ResponseHandler < String > responseHandler = response - > {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
};
Send basic POST request via execute() Method
String responseBody = httpclient.execute(httpDelete, responseHandler);
HttpClient HTTP DELETE Request Method Example
Let's discuss how to use HttpClient in real-time projects. Consider we have deployed Spring boot Restful CRUD APIs. Check out this article - Spring Boot 2 + hibernate 5 + CRUD REST API Tutorial.
In the following example, we are using http://localhost:8080/api/v1/users/5 Rest service to delete a user from a database with id 5. In this example, we are using Java 7 try-with-resources to automatically handle the closing of the ClosableHttpClient and we are also using Java 8 lambdas for the ResponseHandler.
package com.javadevelopersguide.httpclient.examples;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
* This example demonstrates the use of {@link HttpDelete} request method.
*
* @author Ramesh Fadatare
*/
public class DELETERequestExample {
public static void main(String[] args) throws IOException {
deleteUser();
}
public static void deleteUser() throws IOException {
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
HttpDelete httpDelete = new HttpDelete("http://localhost:8080/api/v1/users/5");
System.out.println("Executing request " + httpDelete.getRequestLine());
// Create a custom response handler
ResponseHandler < String > responseHandler = response - > {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
};
String responseBody = httpclient.execute(httpDelete, responseHandler);
System.out.println("----------------------------------------");
System.out.println(responseBody);
}
}
}
Output
Executing request DELETE http://localhost:8080/api/v1/users/5 HTTP/1.1
----------------------------------------
{"deleted":true}
More Examples
In the following example, we demonstrate the HTTP Delete request method by making an HTTP Delete Request Method to the following resource: http://httpbin.org/delete.
package com.javadevelopersguide.httpclient.examples;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
* This example demonstrates the use of {@link HttpDelete} request method.
*
* @author Ramesh Fadatare
*/
public class DELETERequestExample {
public static void main(String[] args) throws IOException {
delete();
}
public static void delete() throws IOException {
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
HttpDelete httpDelete = new HttpDelete("http://httpbin.org/delete");
System.out.println("Executing request " + httpDelete.getRequestLine());
// Create a custom response handler
ResponseHandler < String > responseHandler = response - > {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
};
String responseBody = httpclient.execute(httpDelete, responseHandler);
System.out.println("----------------------------------------");
System.out.println(responseBody);
}
}
}
Output
Executing request DELETE http://httpbin.org/delete HTTP/1.1
----------------------------------------
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept-Encoding": "gzip,deflate",
"Connection": "close",
"Content-Length": "0",
"Host": "httpbin.org",
"User-Agent": "Apache-HttpClient/4.5 (Java/1.8.0_172)"
},
"json": null,
"origin": "49.35.12.218",
"url": "http://httpbin.org/delete"
}
References
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course