如何在 Tornado 中返回没有默认模板的 HTTP 错误代码? [英] How do I return HTTP error code without default template in Tornado?

查看:31
本文介绍了如何在 Tornado 中返回没有默认模板的 HTTP 错误代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用以下内容来提出 HTTP 错误请求:

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

raise tornado.web.HTTPError(400)

返回一个 html 输出:

which returns a html output:

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

是否可以只返回带有自定义正文的 HTTP 响应代码?

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

推荐答案

您可以模拟 RequestHandler.send_error 方法:

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

这篇关于如何在 Tornado 中返回没有默认模板的 HTTP 错误代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆