0
0
Flaskframework~20 mins

After_request hooks in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
After_request Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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
AThe response will not include any 'X-Hello' header.
BThe response will include header 'X-Hello' with value 'Hello'.
CThe response will include header 'X-Hello' with value 'World'.
DThe code will raise a runtime error because after_request must return a string.
Attempts:
2 left
💡 Hint
Remember that after_request modifies the response object before sending it.
lifecycle
intermediate
1:30remaining
When is the after_request hook called in Flask?
In the Flask request lifecycle, at what point is the after_request function executed?
ABefore the view function is called.
BAfter the view function returns but before the response is sent to the client.
COnly if the view function raises an exception.
DAfter the response is sent to the client.
Attempts:
2 left
💡 Hint
Think about when you want to modify the response before the client sees it.
🔧 Debug
advanced
2: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'
ABecause after_request must return a response object, not a string.
BBecause the after_request function must accept no arguments.
CBecause the home route returns a string instead of a response object.
DBecause the after_request function is missing the @app.route decorator.
Attempts:
2 left
💡 Hint
Check what type the after_request function returns.
📝 Syntax
advanced
2: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'.
A
@app.after_request
def add_header():
    response.headers['X-Test'] = 'Yes'
    return response
B
@app.after_request()
def add_header():
    response.headers['X-Test'] = 'Yes'
    return response
C
@app.after_request
def add_header(response):
    response['X-Test'] = 'Yes'
    return response
D
@app.after_request
def add_header(response):
    response.headers['X-Test'] = 'Yes'
    return response
Attempts:
2 left
💡 Hint
Remember the after_request function must accept the response argument and return it.
state_output
expert
2: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'
A'X-Count' header will be '2'.
B'X-Count' header will be '1'.
C'X-Count' header will be '0'.
DThe code will raise a KeyError.
Attempts:
2 left
💡 Hint
Consider the order in which after_request hooks run and how headers are updated.