Updated April 3, 2023
Introduction to Flask make_response
Flask makes response is defined as a utility function that enables users to add additional HTTP headers to the response within the view’s code. This utility function is present in one of the most widely used modules in Flask i.e. the helper’s module that enables the capability to implement various helpers. In some conditions, the return object from views is nothing and it becomes difficult and unclear as to what headers need to be added and for the same use case, this utility function of make response helps developers to tackle the problem of uncertainty of headers and solves the specific problem. In this topic, we are going to learn about Flask make_response.
Syntax:
Now in this section, we will have a glimpse of the syntax for some of the widely used commands in the flask to make a response. Even though this function is under helpers, in python we might see a lot of times that importing the function is from Flask and not from the flask.helpers. the reason we understand the syntax before understanding the working is that having a superficial understanding of syntax will enable easy grasping of the entire context of make response:
Importing make response module in python:
from flask import make_response
Return a response with the helper function in python:
return make_response(<message to be sent>, <success or failure code>)
Return a response from json object with the helper function in python:
return make_response(jsonify(<json object> = <key>), <success or failure code>)
Return a response bypassing *args and **kwargs in python using decorators:
resp = make_response(f(*args, **kwargs))
How does make response work in Flask?
By now we have a fair understanding of what makes response is used for in Flask, but just for the sake of this section, to re-iterate again it enables developers to construct headers for some things which don’t send a response in the view function. This helper function returns the view function to an instance of the response_class. Oh, Wait! Too many new things again? Let us break each of the big terminologies into something smaller, so that we can get a components’ picture in make_response modules and then join them together to effectively learn about the working of make response in Flask.
When a flask application sends requests the portion of the codes known as view functions respond to that response. The incoming URL requests are matched through patterns that Flask uses and relays it to the views for it to handle. Once the views receive the data it is sent back to the Flask which is then turned into an outgoing response. In some circumstances, this response can be None and that is when the make_response is used for. The response_class is a utility that Flask internally uses to containerize the response data that is sent from this view function. This class includes some additional data that is essential for creating the HTTP response in case the response from the view function is sent as None.
The working of Flask response is critical in understanding when we take the final dig at the make_response. The Flask response is a cycle that starts when a response is returned by the function in Flask which handles the request. Generally, a web application render_template is a function where all the routes typically end by a call to that. This function “render_template” renders the referenced template file and post which a string is returned. The route handler function that is responsible for calling render_template, optionally returns 2 more values, namely response code, and custom HTTP headers. Now since the HTTP headers are optional, sometimes these header values are not returned. The Flask application now takes the render_template and creates an instance of the make_response function and then populates the Response object. The reason why HTTP headers are important is that it allows the webserver to be configured in a way to improve security.
One important thing to remember is that the make_response function is capable of taking only one argument and hence if one needs to send the data, status code, and headers it should be sent as a tuple. When the required data is sent, the function populates it with the sent data and sends a response that is in accordance with the details sent. Any of the optional arguments, if missing, is defaulted to the parameters that are within the make_response function. For information, the default status code is 200.
It is now time for us to look at the examples!
Examples of Flask make_response
In the below examples we will look at how to instantiate a make_repsonse in the Flask application. Then look at how does a plain simple text make_response looks like. This will contain the default status code of 200. Next, we will try to modify the default status code and depict how a modified status code is reflected when a response is requested for.
Example #1
Importing make response module in python
Syntax
from flask import Flask
from flask import make_response
app = Flask(__name__)
make_response
Output:
Example #2
Use a by default status code to a make_response instance
Syntax
from flask import Flask
from flask import make_response
app = Flask(__name__)
app.make_response("Hello World")
Output:
Example #3
Use a custom status code (2709) to a make_response instance Syntax
from flask import Flask
from flask import make_response
app = Flask(__name__)
app.make_response(("Hello World",2709))
Output:
Conclusion
In this article, we have got to know about the full details of what makes the response in Flask is and also gotten into hands-on learning of the change of status code in addition to using the default. Rest is in the hands of the reader to use the make_response in the flask application and enable headers in the application for better security of the web application!
Recommended Articles
This is a guide to Flask make_response. Here we discuss the full details of what makes the response in Flask is and also gotten into hands-on learning of the change of status code in addition to using the default. You may also have a look at the following articles to learn more –