0
0
Flaskframework~5 mins

After_request hooks in Flask - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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 response
Click to reveal answer
What does an after_request function in Flask modify?
AThe database connection
BThe incoming request data
CThe Flask app configuration
DThe response before it is sent to the client
Which decorator is used to register an after_request function?
A@app.after_request
B@app.before_request
C@app.route
D@app.teardown_request
What must an after_request function always do with the response?
AReturn the response object
BModify the request object
CClose the Flask app
DRaise an exception
If multiple after_request functions exist, how are they executed?
AOnly the first one runs
BThey run in the order they were registered
CThey run in reverse order
DThey run simultaneously
Which of these is a valid use of an after_request hook?
AModifying incoming form data
BChanging the URL route
CLogging response time
DStarting the Flask server
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.