Brais Gabin
Brais Gabin

Reputation: 5935

How can I perform a HTTP DELETE request with body?

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.

Upvotes: 1

Views: 7146

Answers (2)

dazito
dazito

Reputation: 7980

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

Upvotes: 4

Ishan Liyanage
Ishan Liyanage

Reputation: 2407

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.

Upvotes: 3

Related Questions