Challenge - 5 Problems
After_request Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Flask after_request hook do?
Consider this Flask app code with an after_request hook. What will be the response header 'X-Hello' value for any request?
Flask
from flask import Flask, Response app = Flask(__name__) @app.after_request def add_header(response: Response): response.headers['X-Hello'] = 'World' return response @app.route('/') def index(): return 'Hi!' # Assume app runs normally
Attempts:
2 left
💡 Hint
Remember that after_request modifies the response object before sending it.
✗ Incorrect
The after_request hook modifies the response headers by adding 'X-Hello' with value 'World'. This header will be present in every response.
❓ lifecycle
intermediate1:30remaining
When is the after_request hook called in Flask?
In the Flask request lifecycle, at what point is the after_request function executed?
Attempts:
2 left
💡 Hint
Think about when you want to modify the response before the client sees it.
✗ Incorrect
The after_request hook runs after the view function returns a response but before Flask sends it to the client. It allows modifying the response.
🔧 Debug
advanced2:00remaining
Why does this after_request hook cause an error?
Examine this code snippet. Why will it cause an error when a request is made?
Flask
from flask import Flask app = Flask(__name__) @app.after_request def faulty_hook(response): return 'Not a response object' @app.route('/') def home(): return 'Hello'
Attempts:
2 left
💡 Hint
Check what type the after_request function returns.
✗ Incorrect
The after_request hook must return a valid response object. Returning a string causes Flask to raise a TypeError.
📝 Syntax
advanced2:00remaining
Which after_request hook syntax is correct?
Choose the correct syntax for an after_request hook that adds a header 'X-Test' with value 'Yes'.
Attempts:
2 left
💡 Hint
Remember the after_request function must accept the response argument and return it.
✗ Incorrect
Option D correctly defines the after_request hook with the response parameter and modifies headers properly.
❓ state_output
expert2:30remaining
What is the final response header after multiple after_request hooks?
Given this Flask app with two after_request hooks, what is the final value of the 'X-Count' header in the response?
Flask
from flask import Flask, Response app = Flask(__name__) @app.after_request def first_hook(response: Response): response.headers['X-Count'] = '1' return response @app.after_request def second_hook(response: Response): current = int(response.headers.get('X-Count', '0')) response.headers['X-Count'] = str(current + 1) return response @app.route('/') def index(): return 'Test'
Attempts:
2 left
💡 Hint
Consider the order in which after_request hooks run and how headers are updated.
✗ Incorrect
Flask calls after_request hooks in the order they are registered. The first sets 'X-Count' to '1', the second increments it to '2'.