1

I want to perform a HTTP DELETE request with a xml in the body (not a form). How can I perform this HTTP DELETE request?

I tried with HttpURLConnection but there are some limitations with it.

I tried with the apache's HttpClient too. But I don't know how to send the xml without the structure of application/x-www-form-urlencoded.

Community
  • 1
  • 1
Brais Gabin
  • 5,827
  • 6
  • 57
  • 92
  • 1
    Well in the REST based api http request body is supported ..But i didn't find it anywhere implemented the request body for delete.Hmm interesting – Lithu T.V Feb 26 '13 at 19:09
  • possible duplicate of [HttpDelete with body](http://stackoverflow.com/questions/3773338/httpdelete-with-body) – Marcin Orlowski Apr 08 '13 at 13:49
  • @WebnetMobile, It's not a duplicate. [HttpDelete with body](http://stackoverflow.com/a/3820549/842697) solves the problem if you want send a ````application/x-www-form-urlencoded```` structure but doesn't work with raw data (like a xml). – Brais Gabin Apr 08 '13 at 17:00
  • Does this answer your question? [HttpDelete with body](https://stackoverflow.com/questions/3773338/httpdelete-with-body) – Jacob van Lingen Dec 14 '20 at 15:32

2 Answers2

4

This code will make it:

try {
        HttpEntity entity = new StringEntity(jsonArray.toString());
        HttpClient httpClient = new DefaultHttpClient();
        HttpDeleteWithBody httpDeleteWithBody = new HttpDeleteWithBody("http://10.17.1.72:8080/contacts");
        httpDeleteWithBody.setEntity(entity);

        HttpResponse response = httpClient.execute(httpDeleteWithBody);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

To access the response you can simply do: response.getStatusLine();

And here is the HttpDeleteWithBody class definition: HttpDeleteWithBody

dazito
  • 7,740
  • 15
  • 75
  • 117
3

why not do this with HttpClient

class MyDelete extends HttpPost{
    public MyDelete(String url){
        super(url);
    }
    @Override
    public String getMethod() {
        return "DELETE";
    }

}

it works well.

Ishan Liyanage
  • 2,237
  • 1
  • 26
  • 25