0
0
Flaskframework~20 mins

Response headers in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Setting Custom Response Headers in Flask
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use the standard Flask app run block to start the server.