LoginSignup
1
1

More than 5 years have passed since last update.

webapp2でRailsの_methodパラメータをまねてみる

Posted at

default_dispatcherが呼ばれる前に、_methodパラメータを確認して、request.methodの値を該当するメソッドに書き換えればいいので、自前のdispatcherを用意してapp.router.set_dispatcherメソッドでセットしてやる。

def dispatcher(router, request, response):
    method = request.get('_method').upper()
    if method in ['HEAD', 'OPTIONS', 'PUT', 'DELETE', 'TRACE']:
        request.method = method
    return router.default_dispatcher(request, response)

app = webapp2.WSGIApplication([
    webapp2.Route('/foo', handler=FooHandler),
], debug=True)
app.router.set_dispatcher(dispatcher)
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1