SetEntity in Httpdelete

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?

1

5 Answers

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.

1
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;
    }
3

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.

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;
}

}

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)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest