Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

flask
python
communitycreator

What is Flask.make_response?

Shubham Singh Kshatriya

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Every web application or website that you come across today sends data using a response.

Flaskbased on Python to create web applications is one such framework that does this as well. Before diving into Flask.make_response, let’s first understand what a typical Flask response is.

Typical Flask response

The following code contains a route and a view. It will return a response.

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
  return 'Response'

When you visit the site, you can find an object, named Response, in the body of the response. Along with this, you will also find some headers.

You can use POSTMAN for a better UI to understand these things.

Although the default response object serves a great deal, a problem arises when we want to customize this response. For example, what would you do in a scenario where you want to change the status_code during a particular response or where you want to add some more headers?

Flask.make_response()

Flask provides a method called make_response() that we can use to send custom headers, as well as change the property (like status_code, mimetype, etc.) in response.

from flask import Flask, make_response

app = Flask(__name__)

@app.route('/')
def index():
  myResponse = make_response('Response')
  myResponse.headers['customHeader'] = 'This is a custom header'
  myResponse.status_code = 403
  myResponse.mimetype = 'video/mp4'

  return myResponse

We can import make_response from the flask. make_response() accepts a string as a parameter, then creates and returns a response object. Using this response object, we can add our very own custom headers. We can also change the existing properties as per our needs.

If you check the response object of our site again, you will find the custom headers that we added. You will also find that the status code has changed to 403 from the previous 200.

So, this is how you can customize the response in Flask and include the additional information that you want.

You can connect with me on Twitter for any discussion or questions.

RELATED TAGS

flask
python
communitycreator

CONTRIBUTOR

Shubham Singh Kshatriya

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring