Free public REST API

Real REST API which is ready to handle your HTTP requests 24/7 for free. Can be used for your demo projects, testing, learning or even educating someone else. This REST API supports main HTTP methods such as GET, POST, PUT, DELETE and PATCH.


Send a DELETE request in Java


What is a DELETE Request?

A DELETE Request is one of the HTTP methods which is used when you want to delete a resource from a server. Typically, the resource which can be deleted would be identified by a unique ID or a combination of IDs.


1. How to send a DELETE request in Java?

1.1. Preparing a request

HttpRequest request = HttpRequest.newBuilder() .DELETE() .uri(URI.create("https://api.restful-api.dev/objects/7")) .build();


In order to send a DELETE request in Java, we can use the HttpRequest class to prepare our DELETE request. However, one thing to keep in mind is that HttpRequest class was introduced in Java 11 version. So, if let's say for some reason you are using Java 8 version in 2023, this approach would not work for you.

In the code snippet above you can see that we are specifying that we want to send exactly a DELETE request since it is not a default method. Also we are passing the request URL where number 7 is a unique ID of a resource that we want to delete (P.S. You will need to create your own resource by sending a POST request since a resource with ID = 7 is a reserved ID and is used here just for an example purpose).


1.2. Sending a request and printing a Response

HttpResponse<String> response = HttpClient.newHttpClient() .send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.statusCode()); System.out.println(response.body());


The HttpClient class (which also was introduced in Java 11 version) can be leveraged to send a request that we have prepared in the previous section. If you want, you can print the response code and a body of a DELETE request response to validate that your request was successful.

Depending on a REST API, valid response codes for a DELETE request would be either 200, 202, or 204. The response body is optional and might be empty or contain a message or something like this.


1.3. HTTP DELETE Response codes

200 OK: indicates that the resource has been successfully deleted from the server.

202 Accepted: indicates that the server has received the request and has begun processing it, but the deletion may not have been completed yet.

204 No Content: indicates that the resource has been successfully deleted, but there is no response body returned.


1.4. Putting everything together

import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; public class Main { public static void main(String[] args) throws Exception { HttpRequest request = HttpRequest.newBuilder() .DELETE() .uri(URI.create("https://api.restful-api.dev/objects/7")) .build(); HttpResponse<String> response = HttpClient.newHttpClient() .send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.statusCode()); System.out.println(response.body()); } }