Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask Response vs Flask make_response

Tags:

python

flask

I know that Response is what Flask uses by default for http responses. For specific responses format, I usually use make_response. Are there any specific cases where we have to use flask.Response instead of flask.make_response? in other words, does flask.make_response work in all situations or sometimes flask.Response is the only or the better alternative? Thanks!

like image 866
ettanany Avatar asked Oct 24 '16 11:10

ettanany


1 Answers

make_response() gives you a convenience interface, one that will take arguments in more formats than the flask.Response() object takes.

In addition, make_response() uses the Flask.response_class attribute (so app.response_class) to build the response. The default value for Flask.response_class is flask.Response, but you can set this value to a subclass instead.

So you should really always use make_response(), rather than use flask.Response() directly, especially if you want to support swapping out what Response class you actually use for responses.

You may have to use app.response_class directly if it is a subclass that takes arguments that make_response() can't supply.

like image 92
Martijn Pieters Avatar answered Sep 21 '22 10:09

Martijn Pieters