Search code examples
pythontornado

How do I return HTTP error code without default template in Tornado?


I am currently using the following to raise a HTTP bad request:

raise tornado.web.HTTPError(400)

which returns a html output:

<html><title>400: Bad Request</title><body>400: Bad Request</body></html>

Is it possible to return just the HTTP response code with a custom body?


Solution

  • You may simulate RequestHandler.send_error method:

    class MyHandler(tornado.web.RequestHandler):
        def get(self):
            self.clear()
            self.set_status(400)
            self.finish("<html><body>My custom body</body></html>")