httpbuilder icon indicating copy to clipboard operation
httpbuilder copied to clipboard

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

Open olivermt opened this issue 9 years ago • 7 comments

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?

olivermt avatar Apr 24 '15 08:04 olivermt

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

jonpeterson avatar May 29 '15 02:05 jonpeterson

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

tankchintan avatar Jan 14 '16 00:01 tankchintan

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

Hubbitus avatar Jan 24 '16 09:01 Hubbitus

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

rtretyak avatar Mar 31 '17 12:03 rtretyak

@rtretyak Thanks for your compact solution. very helpful.

nkumarclm avatar Mar 27 '18 11:03 nkumarclm

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.

arpithavaddu avatar Sep 24 '18 13:09 arpithavaddu

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

rtretyak avatar Sep 24 '18 13:09 rtretyak