0
0
Flaskframework~10 mins

Before_request hooks in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Before_request hooks
Client sends HTTP request
Flask receives request
Run before_request hooks
Hook 1
If hook returns response?
YesSend that response back
No
Run view function
Send view response back
When Flask gets a request, it runs all before_request hooks first. If any hook returns a response, Flask sends it immediately. Otherwise, it runs the main view function.
Execution Sample
Flask
from flask import Flask, request
app = Flask(__name__)

@app.before_request
def check_auth():
    if not request.headers.get('Auth'):
        return 'Unauthorized', 401
This code runs check_auth before every request. If 'Auth' header is missing, it stops and returns 401.
Execution Table
StepActionRequest HeadersHook check_auth resultNext StepResponse Sent
1Receive request{'Auth': 'token123'}NoneRun before_request hookNo
2Run before_request hook{'Auth': 'token123'}NoneRun view functionNo
3Run view function{'Auth': 'token123'}NoneSend view responseNo
4Send response{'Auth': 'token123'}NoneDoneYes (view response)
5Receive request{} (no Auth)Returns ('Unauthorized', 401)Send hook responseYes (401 Unauthorized)
💡 Execution stops early if before_request hook returns a response; otherwise, view function runs.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
request.headers{}{'Auth': 'token123'}{'Auth': 'token123'}{'Auth': 'token123'}{'Auth': 'token123'}
hook_resultNoneNoneNoneNoneNone
response_sentNoNoNoYes (view response)Yes (view response)
Key Moments - 2 Insights
Why does Flask stop running the view function when a before_request hook returns a response?
Because the before_request hook returned a response (see execution_table step 5), Flask sends that response immediately and skips the view function.
What happens if no before_request hook returns a response?
Flask continues to run the view function normally (see execution_table steps 1-4), then sends the view's response.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the hook_result at step 2 when 'Auth' header is present?
AError
B'Unauthorized', 401
CNone
DView function response
💡 Hint
Check the 'Hook check_auth result' column at step 2 in execution_table.
At which step does Flask send the response immediately due to a before_request hook?
AStep 3
BStep 5
CStep 1
DStep 4
💡 Hint
Look for the step where 'Response Sent' is 'Yes (401 Unauthorized)' in execution_table.
If the before_request hook never returns a response, what changes in the execution_table?
AView function runs and response is sent at step 4
BFlask skips the view function
CHook result shows a response
DResponse is sent at step 5
💡 Hint
Refer to the normal flow rows where hook_result is None and view function runs.
Concept Snapshot
Flask before_request hooks run before each request.
If a hook returns a response, Flask sends it immediately.
Otherwise, Flask runs the view function.
Use @app.before_request decorator.
Common for auth checks or setup.
Stops request early if needed.
Full Transcript
In Flask, before_request hooks run before the main view function for every incoming request. If any hook returns a response, Flask immediately sends that response and skips the view function. Otherwise, the view function runs normally and its response is sent. This lets you run checks or setup code before handling requests. For example, you can check if an authorization header exists and return a 401 error early if missing. The execution table shows how Flask processes requests step-by-step, running hooks first, then the view, and sending the response. If a hook returns a response, Flask stops early and sends it. This helps control request flow simply and clearly.