Apache HttpClient 4.5 HTTP DELETE Request Method Example
This tutorial demonstrates how to use Apache HttpClient 4.5 to make a Http DELETE request. The HTTP DELETE Request Method requests deletes the resource specified by the URI.
Maven dependencies
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.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.memorynotfound.apache.httpclient.httmethods</groupId>
<artifactId>http-delete</artifactId>
<version>1.0.0-SNAPSHOT</version>
<url>https://memorynotfound.com</url>
<name>httpclient - ${project.artifactId}</name>
<dependencies>
<!-- Apache HttpClient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
HTTP DELETE Request Method Example
In the following example we demonstrate the Http Delete Request Method by making a HTTP Delete Request Method to the following resouce: http://httpbin.org/delete. This resources acknowledges the data and returns a JSON object which we’ll simply print to the console. Note: we are using Java 7 try-with-resources to automatically handle the closing of the ClosableHttpClient
. Next we are using Java 8 lambdas for the ResponseHandler
. Here we are evaluating the Http Status code, when everything is ok we return the body of the response which we parse to a String
. When the status code is not what we expect, we throw a ClientProtocolException
, indicating that the Http DELETE request method failed. Finally, we print the response body to the console.
package com.memorynotfound.httpclient;
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;
import java.io.IOException;
/**
* This example demonstrates the use of {@link HttpDelete} request method.
*/
public class HttpDeleteRequestMethodExample {
public static void main(String... args) 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.2 (Java/1.8.0_20)"
},
"json": null,
"origin": "123.123.123.123",
"url": "http://httpbin.org/delete"
}