0

I have implemeted simple routing:

    return RouterFunctions
            .nest(path("/api/person"),
                    route(GET("/"), personService::findAllPeople)
                            .andRoute(GET("/{id}"), personService::findOnePerson)
                            .andRoute(POST("/add"), personService::addPerson)
                            .andRoute(DELETE("/delete/{id}"), personService::deletePerson)
                            .andRoute(PUT("/update"), personService::updatePerson));

The most interesting method is DELETE becuase it only works via Postman. When I try to type /api/person/delete/1 in browser it throws 404 with none errors in console - does anyone know why? In Postman I disabled all headers which are sent and still Postman works, browser not.

Simon Baslé
  • 27,105
  • 5
  • 69
  • 70
Jan Testowy
  • 649
  • 3
  • 13
  • 32
  • "...try to type /api/person/delete/1..." where are you typing this? – Paul Abbott Feb 12 '18 at 22:29
  • I type it in the url input in browser – Jan Testowy Feb 12 '18 at 22:36
  • 7
    That's not going to work; your browser is going to send the request using the GET verb, not DELETE. Since you have no matching GET route, it returns 404. You really, really do not want something to be deleted via a GET request regardless. – Paul Abbott Feb 12 '18 at 22:39
  • So generally I will not be able to do it via browser? Or should I create new route instead of andRoute to configure delete method? – Jan Testowy Feb 12 '18 at 22:42
  • Browsers only support GET and POST methods. When you type in the URL of the browser, it is GET. When it is a form, it is POST. There is some technique in some web frameworks called *method spoofing*, it is "marking" the method manually as PUT, DELETE, or PATCH, while really and technically it is POST. – user9335240 Feb 12 '18 at 22:42
  • 3
    You need to send a DELETE request via javascript/ajax or whatever else is consuming your API. See https://stackoverflow.com/a/786074/123422 for reasons why. – Paul Abbott Feb 12 '18 at 22:44
  • On the browser you can't directly. But you can do it using AJAX requests. Open your GET URL. Open the JavaScript console on your browser. Then initiate an AJAX request to your other methods. But you will need jQuery (without jQuery, it is a lot of code) – user9335240 Feb 12 '18 at 22:44
  • A browser can only send a GET request and since GET to be idempotent hence if Fired from the browser it won't change the resource state on the server. – Amit Kumar Lal Feb 13 '18 at 07:08
  • 1
    @PaulAbbott could you make this comment an answer for this? Reading questions that are answered in the comments really is a waste of time. – Brian Clozel Feb 13 '18 at 09:14

1 Answers1

2

As @PaulAbbot said, entering an URL in your browser's address bar will issue a GET request only.

If you wish to send DELETE requests from your browser, you should use JavaScript to do so.

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176