【问题标题】:return text/html content-type when using flask-restful使用 flask-restful 时返回 text/html 内容类型
【发布时间】:2014-12-15 20:03:36
【问题描述】:

在特定情况下,我想使用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 content-type 响应?

【问题讨论】:

    标签: python httpresponse content-type flask-restful


    【解决方案1】:

    您必须使用 flask.make_response() 来返回“预烘焙”响应对象:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-17
      • 1970-01-01
      • 2018-10-29
      • 2017-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多