Search code examples
javagwtrestlet

Browser sends OPTIONS instead of PUT or DELETE


I'm using GWT with Restlet and want to send a delete to my Restserver. This is my Client Code for sending:

private void recipeDeleteButton() {
    String id = views.getViewRecipeDetail().getRecipeId().getText();
    ClientResource res = new ClientResource(Rest.RECIPE_CLIENT_RESOURCE_LOCAL + id);
    res.setOnResponse(new Uniform() {

        @Override
        public void handle(Request request, Response response) {
            System.out.println(response.getStatus().getDescription());
        }

    });
    res.delete();
}

But when I want to test this my Firefox says:

[22:28:28.796] OPTIONS http://localhost:8182/recipe/35 [HTTP/1.1 405 Method Not Allowed 3ms]

I have found already a post here: browsers send Options instead of Post after changing http header but this does not solve my problem. For POST and GET it works but only with an Firefox Addon to resolve the problem with SOP, there is also the possiblity to add new headers, but adding PUT,DELETE and OPTIONS does not resolve this problem..

Does anyone know a solution for this, thanks in advance

EDIT

I have tried something like this on serverside, but he still comes with 405.

Series<Header> responseHeaders = (Series<Header>) getResponse().getAttributes().get("org.restlet.http.headers");
    if (responseHeaders == null) {
        responseHeaders = new Series<Header>(Header.class);
        responseHeaders.add(new Header("Access-Control-Allow-Origin", "*"));
        responseHeaders.add(new Header("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS"));
        getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders);
    }

Solution

  • Sending OPTION request is expected behavior in case of CORS request. You can add handler for OPTIONS in your Restserver.

    You will need to return valid Access-Control-Allow-Origin and Access-Control-Allow-Methods. Then client will send second request (PUT or DELETE in your case).