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.
Yes it is allowed to include a body on DELETE requests, but it's semantically meaningless.
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.
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.
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.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With