return text/html content-type when using flask-restful

谁说我不能喝 提交于 2019-12-07 11:33:50

问题


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

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

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


回答1:


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

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

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



来源:https://stackoverflow.com/questions/26472433/return-text-html-content-type-when-using-flask-restful

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!