0
0
Flaskframework~10 mins

After_request hooks in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - After_request hooks
Request received
Route function runs
Response object created
After_request hook runs
Modified or original response returned
Response sent to client
After_request hooks run after the route function creates a response, allowing you to modify it before sending it back.
Execution Sample
Flask
from flask import Flask, request
app = Flask(__name__)

@app.after_request
def add_header(response):
    response.headers['X-Hello'] = 'World'
    return response
This code adds a custom header to every response after the route function finishes.
Execution Table
StepActionInput/StateOutput/StateNotes
1Request receivedClient sends GET /Request object createdFlask starts processing request
2Route function runsRequest objectResponse object createdRoute returns a response
3After_request hook runsResponse object without X-Hello headerResponse object with X-Hello: World headerHook adds custom header
4Response sentModified Response objectClient receives response with headerResponse sent back to client
💡 After_request hook modifies response, then response is sent to client
Variable Tracker
VariableStartAfter Step 2After Step 3Final
response.headers{}{...default headers...}{...default headers..., 'X-Hello': 'World'}{...default headers..., 'X-Hello': 'World'}
Key Moments - 3 Insights
Why does the after_request hook receive a response object?
Because after_request hooks run after the route function returns a response, so they get that response to modify or inspect before sending it out (see execution_table step 3).
Can the after_request hook change the response content?
Yes, the hook can modify headers, status code, or body by changing the response object before returning it (execution_table step 3 shows header added).
What happens if the after_request hook does not return the response?
Flask requires the hook to return a response object; otherwise, it will cause an error or no response will be sent (see code sample return statement).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what does the after_request hook add to the response at step 3?
AA new cookie
BA new header 'X-Hello' with value 'World'
CChanges the response status code
DModifies the request object
💡 Hint
Check the 'Output/State' column at step 3 in the execution_table
At which step does the route function create the response object?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column in execution_table for when the response is created
If the after_request hook did not return the response object, what would happen?
AFlask would send the original response anyway
BThe request would be processed twice
CFlask would raise an error or fail to send a response
DNothing changes, the hook is optional
💡 Hint
Refer to key_moments about the importance of returning the response from the hook
Concept Snapshot
After_request hooks run after a route returns a response.
They receive the response object and can modify it.
Always return the response from the hook.
Used to add headers, cookies, or logging.
Runs before sending response to client.
Full Transcript
In Flask, after_request hooks run after the route function finishes and returns a response. The hook receives this response object and can modify it, for example by adding headers. The hook must return the response object, or Flask will error. The modified response is then sent to the client. This lets you change responses globally after routes run.