0
0
Flaskframework~20 mins

Before_request as middleware alternative in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Before_Request Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a Flask before_request function returns a response?

In Flask, if a before_request function returns a response, what is the effect on the request processing?

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

@app.before_request
def check_auth():
    return jsonify({'error': 'Unauthorized'}), 401

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

# What will a client see when accessing '/'?
AThe client receives a 401 JSON error response and the home route is not executed.
BThe client receives an empty response with status 200.
CThe client receives a 500 Internal Server Error due to returning a response in before_request.
DThe client receives 'Welcome!' because the before_request does not affect the route.
Attempts:
2 left
💡 Hint

Think about how Flask handles returned responses in before_request functions.

📝 Syntax
intermediate
2:00remaining
Identify the correct way to register a before_request function in Flask

Which of the following code snippets correctly registers a before_request function in Flask?

A
def check():
    pass
app.before_request(check)
B
@app.before_request()
def check():
    pass
C
@app.before_request
check = lambda: None
D
@app.before_request
def check():
    pass
Attempts:
2 left
💡 Hint

Remember the decorator syntax for Flask hooks.

🔧 Debug
advanced
2:00remaining
Why does this Flask before_request middleware not block unauthorized access?

Consider this Flask app code:

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

@app.before_request
def check_token():
    token = request.headers.get('Authorization')
    if token != 'secret':
        return None

@app.route('/data')
def data():
    return 'Sensitive data'

Why might a client still access '/data' without the correct token?

ABecause the before_request function does not return a response object, Flask ignores it.
BBecause the route '/data' is registered before the before_request function.
CBecause the before_request function returns a string and status code tuple, which Flask accepts and blocks access correctly.
DBecause the client is sending the correct token, so access is allowed.
Attempts:
2 left
💡 Hint

Check the return type of the before_request function when blocking access.

state_output
advanced
2:00remaining
What is the output when using before_request to modify g object in Flask?

Given this Flask app code:

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

@app.before_request
def set_user():
    g.user = 'Alice'

@app.route('/')
def home():
    return f'Hello, {g.user}!'

What will a client see when accessing '/'?

AHello, !
BHello, Alice!
CAttributeError because g.user is not set
D500 Internal Server Error due to missing return
Attempts:
2 left
💡 Hint

Think about how Flask's g object works across requests.

🧠 Conceptual
expert
3:00remaining
How does Flask's before_request compare to middleware in other frameworks?

Which statement best describes the difference between Flask's before_request functions and middleware in frameworks like Express.js or Django?

AFlask's before_request functions are identical to middleware in Express.js and Django, supporting next() calls to pass control.
BFlask does not support middleware; before_request is the only way to intercept requests.
CFlask's before_request functions are simpler hooks that run before each request but do not form a chain of middleware layers like Express.js middleware.
DFlask's before_request functions run after the response is sent, unlike middleware in other frameworks.
Attempts:
2 left
💡 Hint

Consider how control flow works in Flask before_request vs middleware chains.