Search code examples
pythonhttpresponsecontent-typeflask-restful

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


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?


Solution

  • 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.