What is flask.make_response in Python?
HTTP headers are name or value pairs that appear in the request and response messages of
- Authorization
- Accept
- Connection
- Content-type
- Host
At times, full responses might want to be made, but the render_template function only gives the data that was passed through. So, in order to make the view more explicit, the make_response function is used.
It is necessary to add headers in a view and, with this function, it is possible to attach headers and return the response.
This function can be used to convert a function’s return value of a view into a response, which is useful when using view decorators.
A view could look like this:
from flask import render_template
def index():
return render_template('index.html')
Upon adding the make_response function, it becomes:
from flask import render_template, make_response
def index():
response = make_response(render_template('index.html'))
response.headers['Content-type'] = 'hello world'
return response