Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 929
Jonathan Livni Avatar asked Sep 30 '22 14:09

Jonathan Livni


1 Answers

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.

like image 125
Martijn Pieters Avatar answered Oct 02 '22 15:10

Martijn Pieters