当使用Flask-RESTful时,返回文本/HTML内容类型。

5
在某些特定情况下,我希望以如下方式响应错误:text/html内容类型。
class MyResource(Resource):
    def get(self):
        if some_condition:
            return 'bad argument', 400

上面的代码返回了一个 application/json 类型的内容:'"bad argument"'
而非 text/html 类型的内容:'bad argument'

我该如何强制 flask-restful 使用 text/html 类型来响应?

1个回答

4

您需要使用 flask.make_response() 来返回一个“预先制作好”的响应对象:

return flask.make_response('bad argument', 400)

Flask-Restful会完整地传递Response对象,而不是试图将它们转换为特定请求的MIME类型。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接