Closed waiting-for-dev closed 5 years ago
@waiting-for-dev You cannot use Hanami::Router
like that. In the code above you're mounting Rack::MethodOverride
inside the router.
The logic that turns _method
into a meaningful HTTP method for the router is inside the router, so it will never get hit.
What you want to do is this instead:
# frozen_string_literal: true
require "hanami/router"
require "rack/method_override"
use Rack::MethodOverride
router = Hanami::Router.new do
get "/with_get", to: ->(*) { [200, {}, ["with GET"]] }
delete "/with_delete", to: ->(*) { [200, {}, ["with DELETE"]] }
end
run router
Use the implicit builder of config.ru
. Alternatively you can wrap both use Rack::MethodOverride
and run router
into an explicit Rack::Builder
The incoming requests will be handled by Rack::MethodOverride
first, to apply its logic.
Finally the manipulated Rack env can hit the router.
One note regarding cURL examples: by looking at Rack::MethodOverride
code, only POST requests are considered for method overriding.
The correct manual testing would be:
$ curl -i -X POST -d '_method=DELETE' http://localhost:9999/with_delete
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Server: WEBrick/1.4.2 (Ruby/2.6.3/2019-04-16)
Date: Mon, 29 Jul 2019 14:54:06 GMT
Connection: Keep-Alive
with DELETE
Oh! Thanks for the clarification @jodosha , that makes sense :)
It seems it is not possible to route to a rack application using
Rack::MethodOverride
middleware with browser not supported methods likeDELETE
orPUT
.Script reproducing the issue:
Then: