11

Does the HttpDelete contain a method like setEntity(), like HttpPost or HttpPut? When I use HttpPost i do something like this:

httppost.setEntity(new UrlEncodedFormEntity(
    getNameValuePairsForFriends(context, friendID))); 

How can I do this with wtih delete?

ayahya82
  • 374
  • 3
  • 6
  • 18
  • 2
    HttpDelete does not support entity. So you must implent it again. [this is the answer][1] [1]: http://stackoverflow.com/questions/3773338/httpdelete-with-body – nebyan Feb 28 '13 at 09:08

5 Answers5

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

Make your class extend the http delete class over there and during making the object of your class sent entity and you will be able to post the data in httpdelete

HttpResponse httpResponse;
String result = null;
HttpClient httpClient = new DefaultHttpClient();

HttpConnectionParams
        .setConnectionTimeout(httpClient.getParams(), 10000);


MyDelete httpDelete = new MyDelete(urlUnfollowPatientBundle);
StringEntity entity = null;
try {
    entity = new StringEntity(rawData);
    httpDelete.setEntity(entity);
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}

try {

    httpResponse = httpClient.execute(httpDelete);
    if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        HttpEntity httpEntity = httpResponse.getEntity();
        result = EntityUtils.toString(httpEntity);
        status = true;
    }
Pang
  • 9,564
  • 146
  • 81
  • 122
  • Please format your answer correctly. Currently, it is hard to read. Also, please explain it a bit better, it seems to conflict with the accepted answer. – anderas Jul 22 '15 at 11:03
  • You can't do `httpDelete.setEntity`. There is no method `setEntity` for `httpdelete`, that's the problem. – Katie Oct 19 '16 at 22:12
  • @Kayvar, read the code, @user2306383 extends HttpPost and then names the local var `httpPost`, which indeed does have a `setEntity` method – liltitus27 Feb 10 '17 at 20:26
4

I do not believe that HTTP DELETE takes input - I believe it acts like a GET variant.

The implementation provided by HTTP Client seems to support this conjecture as well.

If you are looking to provide a delete with a body, you /might/ want to consider using a POST to a location that accepts a body.

But in answer to your question, no, delete does not accept a body. You can add query parameters but not a body.

Dave G
  • 9,639
  • 36
  • 41
  • According to standard HTTP DELETE does not take input. But you might encounter a server API which requires form data sent in HTTP DELETE method. It's wrong but it happens. – nuoritoveri Jul 05 '17 at 09:19
2

HTTPDelete will not carry any payload.

HttpDelete will just take the uri/url to be delete and issues a DELETE HTTP Header to the said resource.

TheWhiteRabbit
  • 15,480
  • 4
  • 33
  • 57
0

Try this, simply extended HttpDelete:

class HttpDeleteWithBody extends HttpDelete {

private HttpEntity entity;

public HttpDeleteWithBody(String url) {
    super(url);
}

public HttpEntity getEntity() {
    return this.entity;
}

public void setEntity(final HttpEntity entity) {
    this.entity = entity;
}

}

Tejucb
  • 184
  • 4
0

In Scala, the following code works for me to send body via HTTP DELETE

import org.apache.http.client.methods.HttpEntityEnclosingRequestBase
import java.net.URI

class HttpDeleteWithBody(uri: URI) extends HttpEntityEnclosingRequestBase {
  val METHOD_NAME: String = "DELETE"
  def getMethod: String = METHOD_NAME
  super.setURI(uri)
  setURI(uri)
}