jgritman / httpbuilder

315 stars 154 forks source link

"Cannot set a request body for a DELETE method." #44

Open olivermt opened 9 years ago

olivermt commented 9 years ago

http://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request

There is nothing in the spec that says this is disallowed.

Is there any particular reason that this is not implemented?

jonpeterson commented 9 years ago

I ran into this same issue.

As a workaround for this:

public class CustomRESTClient extends RESTClient {

    private static class HttpDeleteWithEntity extends HttpEntityEnclosingRequestBase {
        public final static String METHOD_NAME = "DELETE";

        @Override
        public String getMethod() {
            return METHOD_NAME;
        }
    }

    public CustomRESTClient(Object defaultURI) throws URISyntaxException {
        super(defaultURI);
    }

    @Override
    public Object delete(Map<String,?> args) throws URISyntaxException, ClientProtocolException, IOException {
        return doRequest(new RequestConfigDelegate(args, new HttpDeleteWithEntity(), null));
    }
}
tankchintan commented 8 years ago

@jonpeterson I did as you have mentioned here, but for whatever reason the overridden delete method is not getting called & I still keep getting the error.

import groovyx.net.http.Method;
import groovyx.net.http.RESTClient;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Map;

public class RichRESTClient extends RESTClient {

  private static class HttpDeleteWithEntity extends HttpEntityEnclosingRequestBase {

    @Override
    public String getMethod() {
      return Method.DELETE.name();
    }
  }

  public RichRESTClient(Object defaultURI) throws URISyntaxException {
    super(defaultURI);
  }

  @Override
  public Object delete(Map<String, ?> args) throws URISyntaxException, IOException {
    return this.doRequest(new RequestConfigDelegate(args, new HttpDeleteWithEntity(), null));  
  }
}
Hubbitus commented 8 years ago

@tankchintan, do you use then RichRESTClient instead of RESTClient? That solution help me.

rtretyak commented 7 years ago

In case if somebody's still having this issue, the more compact solution is:

RESTClient.metaClass.delete = { Map<String,?> args ->  
    def deleteRequestWithEntity = [getMethod: { "DELETE" }] as HttpEntityEnclosingRequestBase
    delegate.doRequest(new RequestConfigDelegate(delegate, args, deleteRequestWithEntity, null));
}
nkumarclm commented 6 years ago

@rtretyak Thanks for your compact solution. very helpful.

arpithavaddu commented 6 years ago

Groovyc: unable to resolve class RequestConfigDelegate I am getting this error when I try to use this. RequestConfigDelegate is protected inner class Any suggestions on how to get it working.

rtretyak commented 6 years ago

@arpithavaddu Are you missing the import statement? import groovyx.net.http.HTTPBuilder.RequestConfigDelegate