0
0
Flaskframework~20 mins

Why middleware extends functionality in Flask - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Middleware Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
How does middleware enhance a Flask app?
Middleware in Flask is used to add extra steps during request processing. What is the main way middleware extends functionality?
ABy changing the HTML templates after they are rendered
BBy replacing the Flask app's core routing system entirely
CBy directly modifying the database schema used by the app
DBy intercepting requests and responses to add or modify data before reaching the route or client
Attempts:
2 left
💡 Hint
Think about what middleware can do between the client and the route handler.
component_behavior
intermediate
2:00remaining
What output results from this Flask middleware example?
Given this Flask middleware that adds a header, what will the client receive in the response headers?
Flask
from flask import Flask, request

app = Flask(__name__)

@app.before_request
def before():
    print('Before request')

@app.after_request
def after(response):
    response.headers['X-Custom'] = 'Middleware'
    return response

@app.route('/')
def home():
    return 'Hello'

# Client sends GET /
AResponse includes header 'X-Custom' with value 'Middleware'
BServer raises an error due to missing middleware
CResponse has no 'X-Custom' header
DResponse includes header 'X-Custom' with value 'Hello'
Attempts:
2 left
💡 Hint
Look at the after_request function modifying headers.
lifecycle
advanced
2:00remaining
At which point does middleware run in Flask's request lifecycle?
Consider Flask's request lifecycle. When does middleware typically execute to extend functionality?
AOnly inside the route function itself
BOnly after the response is sent to the client
CBefore the route function is called and after the response is created
DOnly during app startup before any requests
Attempts:
2 left
💡 Hint
Middleware can act both before and after route processing.
🔧 Debug
advanced
2:00remaining
Why does this Flask middleware not add the header as expected?
This middleware tries to add a header but the client never sees it. What is the likely cause? @app.after_request def add_header(response): response.headers['X-Test'] = 'Value' # Missing return statement here @app.route('/') def index(): return 'Hi'
AHeaders cannot be added in after_request, only before_request
BThe after_request function must return the response object to apply changes
CThe route must explicitly add the header, middleware cannot
DFlask does not support custom headers
Attempts:
2 left
💡 Hint
Check what the after_request function returns.
📝 Syntax
expert
3:00remaining
Which middleware code snippet correctly modifies request data in Flask?
You want to add a custom attribute to the request object in Flask middleware. Which snippet is correct?
A
@app.before_request
def modify_request():
    request.custom = 'data'
B
@app.after_request
def modify_request(response):
    request.custom = 'data'
    return response
C
@app.before_request
def modify_request():
    request = 'data'
D
@app.before_request
def modify_request():
    request.headers['custom'] = 'data'
Attempts:
2 left
💡 Hint
Remember how to add attributes to Flask's request object safely.