Setting response header for JSON responses in Flask

In a flask API

Published on · 1 min read · 1 view · 1 reading right now

PYTHON
FLASK

Working with flask, I had to add custom headers when we send a response to the client. I used Flask's make_response function to create a Response object explicitly(otherwise Response object is created automatically by flask).

I used jsonify to convert python's dictionary to JSON data.

First argument to make_response will be the JSON data, and second will be the HTTP response code.

I added headers by modifying the headers data of the response object, but I think you can also add headers directly in make_response call.

Here's the code:

from flask import make_response, jsonify

# ..... other stuff

@app.route('/')
def home_route():
	response = make_response(jsonify({'hello': 'world'}), 200)
	response.headers['Access-Control-Allow-Origin'] = '*' # <- adding header
	return response
0 likes

Other articles