Response headers send extra information from the server to the browser. They help control how the browser handles the response.
Response headers in Flask
from flask import Flask, Response app = Flask(__name__) @app.route('/') def index(): response = Response('Hello, world!') response.headers['Content-Type'] = 'text/plain' response.headers['X-Custom-Header'] = 'MyValue' return response
You create a Response object or use the one Flask gives you.
Headers are key-value pairs you add to the response.headers dictionary.
response.headers['Content-Type'] = 'application/json'
response.headers['Cache-Control'] = 'no-cache'
response.headers.add('Set-Cookie', 'user=123; HttpOnly')
response.headers['Content-Disposition'] = 'attachment; filename="file.txt"'
This Flask app returns a plain text message with custom headers. It tells the browser the content is plain text, adds a custom header, and disables caching.
from flask import Flask, Response app = Flask(__name__) @app.route('/') def index(): response = Response('Welcome to my site!') response.headers['Content-Type'] = 'text/plain' response.headers['X-Powered-By'] = 'Flask' response.headers['Cache-Control'] = 'no-store' return response if __name__ == '__main__': app.run(debug=True)
Headers names are case-insensitive but usually written in Title-Case.
Some headers like Content-Type are important for browsers to understand the response.
Be careful not to overwrite important default headers unless you know why.
Response headers send extra info from server to browser.
They control content type, caching, security, and more.
In Flask, add headers by setting keys on response.headers dictionary.
Practice
Solution
Step 1: Understand what response headers do
Response headers carry extra information about the response, like content type or caching rules.Step 2: Identify their role in Flask
In Flask, response headers are sent from the server to the browser to control how the browser handles the response.Final Answer:
To send extra information from the server to the browser -> Option AQuick Check:
Response headers = extra info sent to browser [OK]
- Confusing headers with server-side data storage
- Thinking headers create HTML content
- Mixing headers with database operations
X-Custom-Header with value Test to a Flask response?Solution
Step 1: Recall Flask response header syntax
In Flask, headers are set by assigning values to keys in the response.headers dictionary.Step 2: Match the correct syntax
Onlyresponse.headers['X-Custom-Header'] = 'Test'uses the correct dictionary assignment syntax.Final Answer:
response.headers['X-Custom-Header'] = 'Test' -> Option DQuick Check:
Use dictionary assignment for headers [OK]
- Using non-existent methods like add_header or set_header
- Calling response.header() which does not exist
- Trying to set headers as function calls
Content-Type header in the response?from flask import Flask, make_response
app = Flask(__name__)
@app.route('/')
def index():
response = make_response('Hello')
response.headers['Content-Type'] = 'application/json'
return responseSolution
Step 1: Check the header assignment in the code
The code setsresponse.headers['Content-Type'] = 'application/json', which overrides the default.Step 2: Understand the effect on response
This means the response will have the Content-Type header set toapplication/json.Final Answer:
application/json -> Option BQuick Check:
Content-Type header = application/json [OK]
- Assuming default Content-Type without checking header override
- Confusing Content-Type with response body content
- Ignoring header assignment line
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
response = 'Hello'
response.headers['X-Test'] = 'Value'
return responseSolution
Step 1: Identify the response type
The response is a plain string, not a response object.Step 2: Understand header assignment requirements
Headers can only be set on response objects, not on strings.Final Answer:
You cannot add headers to a string response directly -> Option AQuick Check:
Headers require response object, not string [OK]
- Trying to set headers on string instead of response
- Thinking header keys must be lowercase
- Believing Flask disallows custom headers
Cache-Control with value no-store to all responses in your Flask app. Which is the best way to do this?Solution
Step 1: Understand the goal
You want to add the header to all responses automatically, not manually in each route.Step 2: Identify Flask feature for global response modification
The@app.after_requestdecorator lets you modify every response before sending it.Step 3: Evaluate options
The@app.after_requestdecorator adds the header to every response automatically, which is efficient and clean.Final Answer:
Use a @app.after_request function to add the header to all responses -> Option CQuick Check:
Use after_request to set headers globally [OK]
- Adding header manually in every route (Option B)
- Trying to set headers in HTML meta tags (Option C)
- Editing Flask source code (Option D)
Ready to prove it?
Pass the test and this pattern moves unknown
