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

S-K' picture S-K' · Dec 5, 2012 · Viewed 35.9k times · Source

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?

Answer

VisioN picture VisioN · Dec 6, 2012

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>")