Complete the code to register a function that runs before each request in Flask.
from flask import Flask app = Flask(__name__) @app.[1] def before(): print("This runs before each request")
The @app.before_request decorator registers a function to run before each request.
Complete the code to abort the request with a 403 error inside a before_request function.
from flask import Flask, abort app = Flask(__name__) @app.before_request def check(): if not authorized(): [1](403)
The abort(403) function stops the request and returns a 403 Forbidden error.
Fix the error in the before_request function to correctly access the request path.
from flask import Flask, request app = Flask(__name__) @app.before_request def log_path(): print("Request path:", request.[1])
The request.path attribute gives the URL path of the current request.
Fill both blanks to create a before_request function that sets a global variable and checks a header.
from flask import Flask, g, request, abort app = Flask(__name__) @app.before_request def before(): g.user = request.headers.get([1]) if g.user is None: [2](401)
The header X-User is checked, and abort(401) stops unauthorized requests.
Fill all three blanks to create a before_request hook that logs the method, path, and aborts if method is POST.
from flask import Flask, request, abort app = Flask(__name__) @app.before_request def check_post(): print("Method:", request.[1]) print("Path:", request.[2]) if request.method == [3]: abort(405)
This code prints the HTTP method and path, then aborts with 405 if the method is POST.