📖 Scenario: You are building a simple web application using Flask. You want to send a custom response header to the client to provide additional information about the response.
🎯 Goal: Create a Flask route that returns a response with a custom header called X-Custom-Header set to MyValue.
📋 What You'll Learn
Create a Flask app instance named app
Define a route /custom-header that returns a response
Add a custom response header X-Custom-Header with value MyValue
💡 Why This Matters
🌍 Real World
Web developers often need to add custom headers to HTTP responses for security, caching, or metadata purposes.
💼 Career
Understanding how to manipulate response headers is essential for backend developers working with web frameworks like Flask.
Progress0 / 4 steps
1
Create the Flask app and route
Create a Flask app instance called app and define a route /custom-header that returns the string 'Hello, World!'.
Flask
Hint
Use Flask(__name__) to create the app and @app.route('/custom-header') to define the route.
2
Import Response from Flask
Import Response from flask to prepare for creating a response object.
Flask
Hint
Add Response to the import statement from flask.
3
Create a Response object with custom header
In the custom_header function, create a Response object with the body 'Hello, World!'. Then add a custom header X-Custom-Header with the value MyValue to the response headers.
Flask
Hint
Create a Response object and set the header using response.headers['X-Custom-Header'] = 'MyValue'.
4
Run the Flask app
Add the code to run the Flask app with app.run() inside the if __name__ == '__main__': block.
Flask
Hint
Use the standard Flask app run block to start the server.
Practice
(1/5)
1. What is the purpose of response headers in a Flask web application?
easy
A. To send extra information from the server to the browser
B. To store user data on the server
C. To write data into the database
D. To create HTML content dynamically
Solution
Step 1: Understand what response headers do
Response headers carry extra information about the response, like content type or caching rules.
Step 2: Identify their role in Flask
In Flask, response headers are sent from the server to the browser to control how the browser handles the response.
Final Answer:
To send extra information from the server to the browser -> Option A
Quick Check:
Response headers = extra info sent to browser [OK]
Hint: Response headers send info from server to browser [OK]
Common Mistakes:
Confusing headers with server-side data storage
Thinking headers create HTML content
Mixing headers with database operations
2. Which of the following is the correct way to add a custom header X-Custom-Header with value Test to a Flask response?
easy
A. response.set_header('X-Custom-Header', 'Test')
B. response.add_header('X-Custom-Header', 'Test')
C. response.header('X-Custom-Header', 'Test')
D. response.headers['X-Custom-Header'] = 'Test'
Solution
Step 1: Recall Flask response header syntax
In Flask, headers are set by assigning values to keys in the response.headers dictionary.
Step 2: Match the correct syntax
Only response.headers['X-Custom-Header'] = 'Test' uses the correct dictionary assignment syntax.
Final Answer:
response.headers['X-Custom-Header'] = 'Test' -> Option D
Quick Check:
Use dictionary assignment for headers [OK]
Hint: Set headers by assigning to response.headers dict [OK]
Common Mistakes:
Using non-existent methods like add_header or set_header
Calling response.header() which does not exist
Trying to set headers as function calls
3. Given this Flask code snippet, what will be the value of the Content-Type header in the response?