📖 Scenario: You are building a simple Flask web app that returns a greeting message. You want to add a feature that modifies the response before it is sent to the user, such as adding a custom header.
🎯 Goal: Create a Flask app with a route that returns a greeting. Then use an after_request hook to add a custom header X-Custom-Header with the value FlaskProject to every response.
📋 What You'll Learn
Create a Flask app instance named app
Define a route /hello that returns the text Hello, Flask!
Create an after_request function named add_custom_header
In add_custom_header, add the header X-Custom-Header with value FlaskProject to the response
Return the modified response from add_custom_header
💡 Why This Matters
🌍 Real World
After_request hooks are useful to add headers like security policies, CORS, or custom metadata to all responses in a web app.
💼 Career
Understanding Flask hooks is important for backend web development roles that use Python and Flask to build APIs and web services.
Progress0 / 4 steps
1
Create the Flask app and a simple route
Import Flask from flask. Create a Flask app instance called app. Define a route /hello that returns the string 'Hello, Flask!'.
Flask
Hint
Use Flask(__name__) to create the app. Use @app.route('/hello') to define the route.
2
Create the after_request hook function
Define a function called add_custom_header that takes a parameter response. This function will be used as an after_request hook.
Flask
Hint
Define a function with one parameter named response and return it.
3
Add the custom header inside the after_request function
Inside the add_custom_header function, add a header X-Custom-Header with the value FlaskProject to the response.headers. Then return the modified response.
Flask
Hint
Use response.headers['X-Custom-Header'] = 'FlaskProject' to add the header.
4
Register the after_request hook with the Flask app
Register the add_custom_header function as an after_request hook on the app by using the @app.after_request decorator above the function definition.
Flask
Hint
Use @app.after_request decorator above the function add_custom_header.
Practice
(1/5)
1. What is the main purpose of an after_request hook in Flask?
easy
A. To initialize the Flask app configuration
B. To handle incoming request data before routing
C. To define URL routes for the application
D. To modify the response before it is sent to the client
Solution
Step 1: Understand the role of after_request hooks
After_request hooks run after the view function but before the response is sent to the client.
Step 2: Identify what after_request hooks modify
They receive the response object and can modify it, such as adding headers or logging.
Final Answer:
To modify the response before it is sent to the client -> Option D
Quick Check:
After_request hooks modify response [OK]
Hint: Remember: after_request changes response, not request [OK]
Common Mistakes:
Confusing after_request with before_request hooks
Thinking after_request handles incoming data
Assuming after_request defines routes
2. Which of the following is the correct syntax to register an after_request hook in Flask?
easy
A. @app.after_request\ndef modify_response(response):\n return response
B. @app.route('/after')\ndef modify_response():\n return 'Done'
C. @app.before_request\ndef modify_response(response):\n return response
D. @app.after_response\ndef modify_response(response):\n return response
Solution
Step 1: Identify the correct decorator for after_request
The correct decorator is @app.after_request to register a function that modifies the response.
Step 2: Check function signature
The function must accept one argument (the response) and return it.
Final Answer:
@app.after_request\ndef modify_response(response):\n return response -> Option A
D. The print statement is not allowed in after_request
Solution
Step 1: Check function signature for after_request
Functions registered with @app.after_request must accept one argument: the response object.
Step 2: Analyze the return value
The function returns a string instead of a response object, which is incorrect.
Step 3: Identify the main error
Missing the response parameter causes Flask to raise an error because it expects the response argument.
Final Answer:
The function must accept a response parameter -> Option C
Quick Check:
after_request functions need response param [OK]
Hint: after_request functions always take response argument [OK]
Common Mistakes:
Omitting the response parameter in after_request function
Returning a string instead of a response object
Confusing after_request with before_request
5. You want to add a security header X-Frame-Options: DENY to all responses except those from the /public route. How can you implement this using after_request hooks?
hard
A. Use before_request to add the header unconditionally
B. Use after_request to check request.path and add header only if path is not '/public'
C. Add the header inside each route function manually
D. Use after_request but always add the header regardless of path
Solution
Step 1: Use after_request to modify all responses
after_request hooks run after every request and can modify the response before sending.
Step 2: Check the request path inside after_request
By accessing request.path, you can conditionally add headers only when the path is not '/public'.
Step 3: Implement conditional header addition
Inside the after_request function, add the header only if request.path != '/public'.
Final Answer:
Use after_request to check request.path and add header only if path is not '/public' -> Option B
Quick Check:
Conditionally add headers in after_request using request.path [OK]
Hint: Check request.path in after_request to add headers conditionally [OK]
Common Mistakes:
Adding headers unconditionally ignoring route
Using before_request which can't modify response headers
Manually adding headers in every route instead of centralized hook
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.