Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpDelete with body

I'm attempting to use an HttpDelete object to invoke a web service's delete method. The web service's code parses JSON from the message's body. However, I'm failing to understand how to add a body to an HttpDelete object. Is there a way to do this?

With HttpPut and HttpPost, I call the setEntity method and pass in my JSON. There doesn't appear to be any such method for HttpDelete.

If there is no way to set a body for an HttpDelete object, could you please link me to a resource that uses a super class of HttpDelete such that I can set the method (delete) and set a body. I know that isn't ideal, but at this point I can't alter the web service.

like image 952
Andrew Avatar asked Sep 22 '10 20:09

Andrew


People also ask

Can an HTTP delete have a body?

Yes it is allowed to include a body on DELETE requests, but it's semantically meaningless.

Can you send a body with a GET request?

So, yes, you can send a body with GET, and no, it is never useful to do so. This is part of the layered design of HTTP/1.1 that will become clear again once the spec is partitioned (work in progress). Yes, you can send a request body with GET but it should not have any meaning.

Does HTTP GET have a body?

Yes. In other words, any HTTP request message is allowed to contain a message body, and thus must parse messages with that in mind. Server semantics for GET, however, are restricted such that a body, if any, has no semantic meaning to the request.

Can HTTP delete have query parameters?

There's nothing wrong with using DELETE on a collection and filtering by query parameters. Neither the REST dissertation nor the HTTP spec say anything about not doing this. This is different than the answer to the question that @Thilo linked to because the circumstances are different.


2 Answers

Have you tried overriding HttpEntityEnclosingRequestBase as follows:

import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; import java.net.URI; import org.apache.http.annotation.NotThreadSafe;  @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(); } } 

That will create a HttpDelete-lookalike that has a setEntity method. I think the abstract class does almost everything for you, so that may be all that's needed.

FWIW, the code is based on this source to HttpPost that Google turned up.

like image 78
Walter Mundt Avatar answered Sep 23 '22 23:09

Walter Mundt


Following Walter Mudnt advice, you can use this code. It works, just made it while testing my REST webservice.

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();

like image 43
dazito Avatar answered Sep 25 '22 23:09

dazito