Recall & Review
beginner
What is an
after_request hook in Flask?An
after_request hook is a function that Flask runs after every request to modify or finalize the response before sending it to the client.Click to reveal answer
beginner
How do you register an
after_request function in Flask?You use the
@app.after_request decorator above a function that takes the response object as a parameter and returns it, possibly modified.Click to reveal answer
beginner
What parameter does an
after_request function receive?It receives the response object that Flask created for the request, allowing you to change headers, cookies, or content before sending it.
Click to reveal answer
intermediate
Can multiple
after_request functions be used in a Flask app?Yes, Flask runs all registered
after_request functions in the order they were added, each receiving the response from the previous one.Click to reveal answer
beginner
Give a simple example of an
after_request hook that adds a custom header.Example:<br>
@app.after_request
def add_header(response):
response.headers['X-Custom-Header'] = 'Hello'
return responseClick to reveal answer
What does an
after_request function in Flask modify?✗ Incorrect
An
after_request function modifies the response object before Flask sends it back to the client.Which decorator is used to register an
after_request function?✗ Incorrect
The
@app.after_request decorator registers a function to run after each request.What must an
after_request function always do with the response?✗ Incorrect
The function must return the response object, possibly modified, so Flask can send it to the client.
If multiple
after_request functions exist, how are they executed?✗ Incorrect
Flask runs
after_request functions in the order they were added, passing the response along.Which of these is a valid use of an
after_request hook?✗ Incorrect
You can use
after_request hooks to log info about the response, like timing or headers.Explain what an
after_request hook does in Flask and how you use it.Think about what happens just before Flask sends data back to the browser.
You got /5 concepts.
Describe a practical example where an
after_request hook would be useful.Consider tasks that happen after your app creates a response but before the user sees it.
You got /5 concepts.