java - DELETE Request with Payload or Form Data causes Bad Request -
i building restful web service java jersey 2.17. client it. developing extjs 5.
my classes on service
main.java
public class main { public static final string base_uri = "http://localhost:8080/app/rest/"; public static httpserver startserver() { final resourceconfig rc = new resourceconfig(); rc.packages(true, "app"); rc.register(responsecorsfilter.class); return grizzlyhttpserverfactory.createhttpserver(uri.create(main.base_uri), rc); } public static void main(final string[] args) throws ioexception { final httpserver server = main.startserver(); system.out.println(string.format("jersey app started wadl available @ " + "%sapplication.wadl\nhit enter stop it...", main.base_uri)); system.in.read(); server.stop(); } } userri.java
@path("/user") public class usersri { @delete @path("/{id}") @produces(mediatype.application_json) public string deleteuser(@pathparam("id") final string id) { final string res = "{\"success\":true,\"msg\":\"user " + id + " deleted.\"}"; return res; } } responsecorsfilter.java
public class responsecorsfilter implements containerresponsefilter { @override public void filter(final containerrequestcontext req, final containerresponsecontext contresp) { contresp.getheaders().add("access-control-allow-headers", "origin, x-requested-with, content-type, accept"); contresp.getheaders().add("access-control-allow-methods", "get, post, put, delete, options"); contresp.getheaders().add("access-control-allow-origin", "*"); final string reqhead = req.getheaderstring("access-control-request-headers"); if ((null != reqhead) && stringutils.isnotblank(reqhead)) { contresp.getheaders().add("access-control-request-headers", reqhead); } } } at moment stuck deleting content. on client record form panel , calling erase function. request/response looking this:
general: remote address:127.0.0.1:8080 request url:http://localhost:8080/app/rest/user/user4 request method:delete status code:400 bad request response headers connection:close content-length:0 date:tue, 14 apr 2015 19:26:05 gmt request headers accept:*/* accept-encoding:gzip, deflate, sdch accept-language:de-de,de;q=0.8,en-us;q=0.6,en;q=0.4 connection:keep-alive content-length:14 content-type:application/json host:localhost:8080 origin:http://localhost referer:http://localhost/app/ user-agent:mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, gecko) chrome/41.0.2272.118 safari/537.36 x-requested-with:xmlhttprequest request payload {"id":"user4"} on console see xmlhttprequest cannot load http://localhost:8080/app/rest/user/user4. no 'access-control-allow-origin' header present on requested resource. origin 'http://localhost' therefore not allowed access. response had http status code 400.
it reproduceable following jquery.ajax() call: $.ajax({method:'delete',url:"http://localhost:8080/app/rest/user/user4",data:{"id":"user4"}})
sending request without form data or payload working.
is there way solve without overriding stuff in extjs framework?
greetings
sören
delete, get requests should not have payload (entity body). don't know if specified anywhere, can see discussion here.
that's what's causing 400 bad request(will occur also). rid of it, , work. anyway there not need send body, are, information id, included in in url path.
if that's example, , need send other arbitrary information along request, use query params, e.g.
/app/rest/user/user4?some=value1&other=value2&data=value3 public string deleteuser(@pathparam("id") final string id, @queryparam("some") string some, @queryparam("other") string other, @queryparam("data") string data) {} with jquery, do
var params = { some:"valeu1", other:"value2", data:"value3" } var encoded = $.param(params); var url = baseurl + "?" + encoded; $.ajax({ url: url, ... }) update
so after investigation, seems grizzly problem. i've tested jetty, , works fine.
see similar issue
update 2
so @alexey stated in comment below
you're right, grizzly default doesn't allow payload http methods, http spec doesn't explicitly state that. http get, delete, head. can switch support on calling:
httpserver.getserverconfiguration().setallowpayloadforundefinedhttpmethods(true);please not method should called before startinghttpserver
so fix like
public static httpserver createserver() { final resourceconfig rc = new resourceconfig(); rc.packages(true, "app"); rc.register(responsecorsfilter.class); httpserver server = grizzlyhttpserverfactory.createhttpserver( uri.create(base_uri), rc, false); server.getserverconfiguration().setallowpayloadforundefinedhttpmethods(true); return server; } public static void main(string[] args) throws ioexception { final httpserver server = createserver(); server.start(); system.out.println(string.format("jersey app started wadl available @ " + "%sapplication.wadl\nhit enter stop it...", base_uri)); system.in.read(); server.stop(); } tested , works expected.
Comments
Post a Comment