0
0
Flaskframework~20 mins

Custom middleware creation in Flask - Practice Problems & Coding Challenges

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!
component_behavior
intermediate
2:00remaining
What does this Flask middleware do to the response?

Consider this Flask middleware that modifies the response body. What will be the final response content when a client requests the root URL?

Flask
from flask import Flask, request, Response

app = Flask(__name__)

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

@app.after_request
def after(response):
    response.set_data(response.get_data(as_text=True) + ' World!')
    return response

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

if __name__ == '__main__':
    app.run()
A"Hello World!"
B"Hello"
C" World!"
DEmpty response
Attempts:
2 left
💡 Hint

Look at how the after_request function changes the response data.

lifecycle
intermediate
1:30remaining
When is Flask middleware executed in the request lifecycle?

Given Flask middleware functions before_request and after_request, when are they executed relative to the route handler?

Abefore_request runs after the route handler; after_request runs before the route handler
Bbefore_request runs before the route handler; after_request runs after the route handler
CBoth run before the route handler
DBoth run after the route handler
Attempts:
2 left
💡 Hint

Think about the names and purpose of these middleware hooks.

📝 Syntax
advanced
2:30remaining
Which middleware code snippet correctly adds a custom header to all responses?

Choose the correct Flask middleware code that adds the header X-Custom: True to every response.

A
@app.after_request
def add_header():
    response.headers['X-Custom'] = 'True'
    return response
B
@app.before_request
def add_header():
    response.headers['X-Custom'] = 'True'
    return response
C
@app.before_request
def add_header(response):
    response.headers['X-Custom'] = 'True'
    return response
D
@app.after_request
def add_header(response):
    response.headers['X-Custom'] = 'True'
    return response
Attempts:
2 left
💡 Hint

Remember which middleware hook receives the response object.

🔧 Debug
advanced
2:00remaining
What does this Flask middleware do when a request is made?

Examine the middleware code below. What happens when a request is made?

Flask
from flask import Flask
app = Flask(__name__)

@app.before_request
def check():
    return 'Blocked'

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

if __name__ == '__main__':
    app.run()
AThe route handler is never called because before_request raises an exception
BThe before_request must return None or a Response object, returning a string causes a TypeError
CThe before_request returns a string which stops the request and returns 'Blocked' instead of calling the route
DThe middleware is missing a return statement, causing a runtime error
Attempts:
2 left
💡 Hint

Check what returning a string from before_request does in Flask.

state_output
expert
3:00remaining
What is the final response content after multiple middleware modify it?

Given these two Flask middleware functions, what is the final response content when requesting '/'?

Flask
from flask import Flask, Response
app = Flask(__name__)

@app.after_request
def add_hello(response):
    response.set_data(response.get_data(as_text=True) + ' Hello')
    return response

@app.after_request
def add_world(response):
    response.set_data(response.get_data(as_text=True) + ' World')
    return response

@app.route('/')
def index():
    return 'Start'

if __name__ == '__main__':
    app.run()
A"Start World Hello"
B"Start Hello"
C"Start Hello World"
D"Start World"
Attempts:
2 left
💡 Hint

Consider the order in which multiple after_request functions run (reverse registration order).