Search code examples
pythonpython-2.7tornado

Tornado POST 405: Method Not Allowed


For some reason, I am not able to use POST methods in tornado.

Even the hello_world example does not work when I change GET to POST.

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def post(self):
        self.write("Hello, world")

application = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

It throws "405 method not allowed". Any suggestions?


Solution

  • You still need get if you want access the page, because access the page using browser request with GET method.

    import tornado.ioloop
    import tornado.web
    
    class MainHandler(tornado.web.RequestHandler):
        def post(self):
            self.write("Hello, world")
        get = post # <--------------
    
    application = tornado.web.Application([
        (r"/", MainHandler),
    ])
    
    if __name__ == "__main__":
        application.listen(8888)
        tornado.ioloop.IOLoop.instance().start()