0
0
Flaskframework~10 mins

Response headers in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Response headers
Client sends HTTP request
Flask app processes request
Create response object
Add headers to response
Send response with headers back to client
Client receives response with headers
This flow shows how a Flask app receives a request, creates a response, adds headers, and sends it back to the client.
Execution Sample
Flask
from flask import Flask, make_response
app = Flask(__name__)

@app.route('/')
def index():
    resp = make_response('Hello World')
    resp.headers['X-Custom-Header'] = 'MyValue'
    return resp
This Flask route returns 'Hello World' with a custom response header 'X-Custom-Header'.
Execution Table
StepActionResponse ContentHeaders BeforeHeaders After
1Request received at '/' route{}{}
2Create response with body 'Hello World'Hello World{}{}
3Add header 'X-Custom-Header: MyValue'Hello World{}{'X-Custom-Header': 'MyValue'}
4Return response to clientHello World{'X-Custom-Header': 'MyValue'}{'X-Custom-Header': 'MyValue'}
💡 Response sent with body and custom header; request handling complete.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
resp.headers{}{}{'X-Custom-Header': 'MyValue'}{'X-Custom-Header': 'MyValue'}
resp.datanullb'Hello World'b'Hello World'b'Hello World'
Key Moments - 2 Insights
Why do we add headers after creating the response object?
Because the response object must exist first to modify its headers, as shown in step 3 of the execution_table.
Can we add multiple headers the same way?
Yes, you can add multiple headers by assigning different keys in resp.headers before returning, similar to step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what header is added to the response?
AX-Custom-Header with value 'MyValue'
BContent-Type with value 'text/html'
CServer with value 'Flask'
DAuthorization with value 'Bearer token'
💡 Hint
Check the 'Headers After' column at step 3 in the execution_table.
At which step does the response body get set to 'Hello World'?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Response Content' column in the execution_table.
If you add another header 'Cache-Control' before returning, how would the 'Headers After' look at step 4?
A{'X-Custom-Header': 'MyValue'}
B{}
C{'X-Custom-Header': 'MyValue', 'Cache-Control': 'no-cache'}
D{'Cache-Control': 'no-cache'}
💡 Hint
Refer to variable_tracker and how headers accumulate after step 3.
Concept Snapshot
Flask Response Headers Quick Guide:
- Create response object with make_response()
- Add headers via resp.headers['Header-Name'] = 'value'
- Return response object from route
- Headers sent with response to client
- Use headers to control caching, security, or custom info
Full Transcript
In Flask, when a client sends a request, the app processes it and creates a response object. We can add headers to this response by modifying resp.headers before returning it. For example, adding a custom header 'X-Custom-Header' with a value. The response body and headers are then sent back to the client. This process allows us to control extra information sent with the response, like caching or security details.