Challenge - 5 Problems
Before_request Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a before_request hook returns a response?
Consider a Flask app with a
before_request hook that returns a response object. What will Flask do next?Flask
from flask import Flask, jsonify app = Flask(__name__) @app.before_request def check(): return jsonify({'error': 'blocked'}) @app.route('/') def home(): return 'Welcome!' # What will be the output when accessing '/'?
Attempts:
2 left
💡 Hint
Think about what happens if a before_request returns a response instead of None.
✗ Incorrect
If a before_request hook returns a response, Flask stops processing further and sends that response immediately. The route handler is skipped.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this before_request hook
Which option contains a syntax error in defining a Flask before_request hook?
Flask
from flask import Flask app = Flask(__name__) @app.before_request def check(): print('Checking request') @app.route('/') def home(): return 'Hello!'
Attempts:
2 left
💡 Hint
Check function definition syntax carefully.
✗ Incorrect
Option C misses parentheses in the function definition (def check:), causing a syntax error.
❓ state_output
advanced2:00remaining
How many times does the before_request hook run on multiple requests?
Given this Flask app, how many times will the
before_request hook run after 3 separate HTTP GET requests to '/'?Flask
from flask import Flask app = Flask(__name__) counter = 0 @app.before_request def increment(): global counter counter += 1 @app.route('/') def home(): return f'Count: {counter}'
Attempts:
2 left
💡 Hint
Think about when before_request hooks run in relation to each HTTP request.
✗ Incorrect
The before_request hook runs before every request, so it runs 3 times for 3 requests.
🔧 Debug
advanced2:00remaining
Why does this before_request hook not block unauthorized access?
This Flask app tries to block access if a header is missing, but unauthorized users still get through. Why?
Flask
from flask import Flask, request, abort app = Flask(__name__) @app.before_request def check_auth(): if 'X-Auth' not in request.headers: abort(403) @app.route('/') def home(): return 'Welcome!'
Attempts:
2 left
💡 Hint
Consider what abort(403) does in Flask.
✗ Incorrect
Calling abort(403) raises an exception that immediately stops the request and returns a 403 response. The route does not run.
🧠 Conceptual
expert2:00remaining
Order of execution with multiple before_request hooks
Given two before_request hooks in this Flask app, in what order do they run?
Flask
from flask import Flask app = Flask(__name__) @app.before_request def first(): print('First hook') @app.before_request def second(): print('Second hook') @app.route('/') def home(): return 'Done'
Attempts:
2 left
💡 Hint
Think about the order decorators are applied in Python.
✗ Incorrect
Flask runs before_request hooks in the order they are defined in the code, so 'first' runs before 'second'.