0
0
Flaskframework~10 mins

Before_request as middleware alternative in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Before_request as middleware alternative
Client sends HTTP request
Flask app receives request
@before_request function runs
Continue to route
Route handler runs
Response sent back to client
This flow shows how Flask runs a function before each request to act like middleware, deciding to continue or return early.
Execution Sample
Flask
from flask import Flask, request
app = Flask(__name__)

@app.before_request
def check_auth():
    if request.headers.get('X-Auth') is None:
        return 'Unauthorized', 401
This code runs check_auth before every request to block unauthorized access if header missing.
Execution Table
StepActionRequest Headersbefore_request ResultRoute Handler CalledResponse Sent
1Request received with headers {'X-Auth': 'token123'}{'X-Auth': 'token123'}None (continue)YesRoute response
2Request received with headers {}{}'Unauthorized', 401 (stop)No'Unauthorized', 401
3Request received with headers {'X-Auth': ''}{'X-Auth': ''}None (continue)YesRoute response
💡 Execution stops early if before_request returns a response, otherwise route handler runs.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
request.headers{}{'X-Auth': 'token123'}{}{'X-Auth': ''}
before_request returnNoneNone'Unauthorized', 401None
route_handler_calledFalseTrueFalseTrue
response_sentNoneRoute response'Unauthorized', 401Route response
Key Moments - 2 Insights
Why does the route handler not run when before_request returns a response?
Because returning a response from before_request stops further processing, as shown in execution_table row 2 where route_handler_called is No.
What happens if before_request returns None?
Returning None means continue processing normally, so the route handler runs, as seen in rows 1 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the before_request return value at step 2?
A'OK', 200
BNone
C'Unauthorized', 401
DRaises error
💡 Hint
Check the 'before_request Result' column at step 2 in the execution_table.
At which step does the route handler NOT get called?
AStep 2
BStep 1
CStep 3
DAll steps call route handler
💡 Hint
Look at the 'Route Handler Called' column in the execution_table.
If the request headers always include 'X-Auth', how would the response_sent column change?
AIt would always be 'Unauthorized', 401
BIt would always be 'Route response'
CIt would alternate between 'Unauthorized' and 'Route response'
DIt would be empty
💡 Hint
Refer to variable_tracker for response_sent values when 'X-Auth' header is present.
Concept Snapshot
Flask @before_request runs a function before each request.
If it returns a response, request stops and response is sent early.
If it returns None, request continues to route handler.
Use it like middleware to check auth or modify requests.
Helps centralize pre-route logic in one place.
Full Transcript
In Flask, the @before_request decorator lets you run a function before every request. This function can check things like authentication headers. If it returns a response, Flask sends that response immediately and skips the route handler. If it returns None, Flask continues to the route handler as usual. This acts like middleware, letting you centralize checks or setup before routes run. For example, checking for an 'X-Auth' header and returning 'Unauthorized' if missing stops the request early. Otherwise, the route runs normally. This flow helps keep your app organized and secure.