使用flask-restful时返回text / html content-type [英] return text/html content-type when using flask-restful

查看:416
本文介绍了使用flask-restful时返回text / html content-type的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在特定情况下,我想使用 text / html 内容类型响应以下错误:

In a specific case I'd like to respond with a text/html content-type for an error as follows:

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

上面的代码返回 application / json 内容-类型:'错误参数'

而不是 text / html 内容类型:错误的论点

The code above returns an application/json content-type: '"bad argument"'
instead of a text/html content-type: 'bad argument'

如何强制烧瓶静止以文本进行响应/ html 内容类型?

How can I force flask-restful to respond with text/html content-type?

推荐答案

您将必须使用 flask.make_response() 返回一个预烘焙响应对象:

You'll have to use flask.make_response() to return a 'pre-baked' response object:

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

Flask-Restful将通过完整的 Response 对象,进行车削而不是尝试将它们转换为请求的特定mime类型。

Flask-Restful will pass full-on Response objects unaltered, rather than try and convert them to a specific requested mime type.

这篇关于使用flask-restful时返回text / html content-type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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