Java HTTP DELETE with Request Body

26,642

Solution 1

I used org.apache.http to get this done.

@NotThreadSafe
class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
    public static final String METHOD_NAME = "DELETE";
    public String getMethod() {
        return METHOD_NAME;
    }
    public HttpDeleteWithBody(final String uri) {
        super();
        setURI(URI.create(uri));
    }
    public HttpDeleteWithBody(final URI uri) {
        super();
        setURI(uri);
    }
    public HttpDeleteWithBody() {
        super();
    }
}
public String[] sendDelete(String URL, String PARAMS, String header) throws IOException {
    String[] restResponse = new String[2];
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpDeleteWithBody httpDelete = new HttpDeleteWithBody(URL);
        StringEntity input = new StringEntity(PARAMS, ContentType.APPLICATION_JSON);
        httpDelete.addHeader("header", header);
        httpDelete.setEntity(input);  
        Header requestHeaders[] = httpDelete.getAllHeaders();
        CloseableHttpResponse response = httpclient.execute(httpDelete);
        restResponse[0] = Integer.toString((response.getStatusLine().getStatusCode()));
        restResponse[1] = EntityUtils.toString(response.getEntity());    
        return restResponse;
    }
}

Solution 2

If you are using Spring, you can use RestTemplate to generate the client request. In this case you could use RestTemplate.exchange and provide the url, http method and request body. Something like (not tested, but you get the idea):

RestTemplate restTemplate = new RestTemplate();
HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"));
restTemplate.exchange(url, HttpMethod.DELETE, request, null);

Solution 3

This code worked for me:-

  • You set content type by httpCon.setRequestProperty
  • You set the request Method by httpCon.setRequestMethod
  • Write the json body into OutputStreamWriter, in my sample, i converted Java object to json using Jackson ObjectMapper

    URL url = new URL("http://localhost:8080/greeting");
    HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
    httpCon.setDoOutput(true);
    httpCon.setRequestProperty(
                    "Content-Type", "application/json");
    httpCon.setRequestMethod("DELETE");
    OutputStreamWriter out = new OutputStreamWriter(
                    httpCon.getOutputStream());
    ObjectMapper objectMapper = new ObjectMapper();
    out.write(objectMapper.writeValueAsString(new Greeting("foo")));
    out.close();
    httpCon.connect();
    
Share:
26,642
Author by

DXBKing

Updated on July 05, 2022

Comments

  • DXBKing 5 months

    I have an external API which uses DELETE with the body(JSON). I make use of Postman REST Client and get the delete done with request body and it works fine. I am trying to automate this functionality using a method.

    I tried HttpURLConnection for similar GET, POST and PUT. But I am not sure how to use the DELETE with a request body.

    I have checked in StackOverflow and see this cannot be done, but they are very old answers.

    Can someone please help? I'm using spring framework.