EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Flask Tutorial Flask make_response
Secondary Sidebar
Flask Tutorial
  • Flask
    • Flask Version
    • Flask make_response
    • Flask WebSocket
    • Flask Session
    • Flask Environment Variables
    • Flask Cache
    • Flask Server
    • Flask jsonify
    • Flask logging
    • Flask redirect
    • Flask wtforms
    • Flask config
    • Flask Users
    • Flask upload file
    • Flask? get post data
    • Flask Template
    • Flask DB Migrate
    • Flask HTTPS
    • Flask bcrypt
    • Flask debug mode
    • Flask authentication
    • Flask Migrate
    • Flask URL Parameters
    • Flask API
    • Flask bootstrap
    • Flask POST request
    • Flask Extensions

Flask make_response

Flask-make_response

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:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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:

Flask make_response output 1

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: Flask make_response output 2

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:

output 3

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 –

  1. HTTP Methods
  2. HTTP Caching
  3. What is URL Link?
  4. JSON Parser
Popular Course in this category
Programming Languages Training (41 Courses, 13+ Projects, 4 Quizzes)
  41 Online Courses |  13 Hands-on Projects |  322+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more