使用flask-restful时返回text / html内容类型

时间:2014-10-20 18:27:09

标签: python httpresponse content-type flask-restful

在特定情况下,我想以text/html内容类型回复错误,如下所示:

class MyResource(Resource):
    def get(self):
        if some_condition:
            return 'bad argument', 400

上面的代码会返回application/json内容类型:'"bad argument"'
而不是text/html内容类型:'bad argument'

如何使用text/html内容类型强制使用burn-restful来回复?

1 个答案:

答案 0 :(得分:3)

您必须使用flask.make_response()返回预先出炉的'响应对象:

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

Flask-Restful将不加改变地传递全部Response个对象,而不是尝试将它们转换为特定的请求mime类型。