0
0
FlaskHow-ToBeginner · 3 min read

How to Set Response Headers in Flask: Simple Guide

In Flask, you can set response headers by modifying the headers attribute of a Response object or by using make_response to create a response and then adding headers. This allows you to customize HTTP headers like Content-Type or Cache-Control before sending the response.
📐

Syntax

To set response headers in Flask, you typically create or modify a Response object and then add headers using the headers dictionary-like attribute.

  • Using make_response: Create a response object from a string or template, then set headers.
  • Directly modifying Response: You can also return a Response object with headers set.
python
from flask import Flask, make_response

app = Flask(__name__)

@app.route('/')
def index():
    response = make_response('Hello, world!')
    response.headers['Custom-Header'] = 'MyValue'
    return response
💻

Example

This example shows how to set a custom header X-Custom-Header and a content type header in a Flask route response.

python
from flask import Flask, make_response

app = Flask(__name__)

@app.route('/')
def hello():
    response = make_response('Hello with headers!')
    response.headers['X-Custom-Header'] = 'MyHeaderValue'
    response.headers['Content-Type'] = 'text/plain; charset=utf-8'
    return response

if __name__ == '__main__':
    app.run(debug=True)
Output
When you visit http://localhost:5000/, the browser receives the text "Hello with headers!" with the headers: X-Custom-Header: MyHeaderValue Content-Type: text/plain; charset=utf-8
⚠️

Common Pitfalls

Common mistakes when setting headers in Flask include:

  • Trying to set headers directly on the string returned by the route instead of a Response object.
  • Overwriting default headers unintentionally, like Content-Type, without specifying the correct value.
  • Not returning the modified Response object, which means headers won't be sent.
python
from flask import Flask

app = Flask(__name__)

@app.route('/wrong')
def wrong():
    # This will NOT set headers because it's just a string
    response = 'Hello'
    # The following line will raise an AttributeError because strings have no headers attribute
    # response.headers = {'X-Fail': 'NoEffect'}  # This does nothing
    return response

@app.route('/right')
def right():
    from flask import make_response
    response = make_response('Hello')
    response.headers['X-Success'] = 'Works'
    return response
📊

Quick Reference

Summary tips for setting response headers in Flask:

  • Use make_response() to create a response object from your content.
  • Modify headers via response.headers['Header-Name'] = 'value'.
  • Always return the Response object, not just a string.
  • Set common headers like Content-Type carefully to avoid issues.

Key Takeaways

Use make_response() to create a response object before setting headers.
Modify headers by assigning values to response.headers dictionary.
Always return the response object, not just a string, to send headers.
Be careful not to overwrite important default headers unintentionally.
Setting headers allows customizing HTTP responses like content type or caching.