Skip to content

Instantly share code, notes, and snippets.

@blixt
Created August 16, 2014 18:24
Show Gist options
  • Save blixt/54d0a8bf9f64ce2ec6b8 to your computer and use it in GitHub Desktop.
Save blixt/54d0a8bf9f64ce2ec6b8 to your computer and use it in GitHub Desktop.
How to add CORS support to a Flask app in 9 lines of code
def add_cors_headers(response):
response.headers['Access-Control-Allow-Origin'] = '*'
if request.method == 'OPTIONS':
response.headers['Access-Control-Allow-Methods'] = 'DELETE, GET, POST, PUT'
headers = request.headers.get('Access-Control-Request-Headers')
if headers:
response.headers['Access-Control-Allow-Headers'] = headers
return response
app.after_request(add_cors_headers)
@valencik
Copy link

This worked wonders, thanks.

@EmilRex
Copy link

EmilRex commented May 10, 2015

This is a life saver! Thank you so much!

@mvgolom
Copy link

mvgolom commented May 6, 2019

This save my life Thanks

@vishal-iitr2003
Copy link

Big thank you for this. :)

@sherwin7
Copy link

can you please explain what is that request is having?

@sherwin7
Copy link

can some one tell me what does that request does?

@takumade
Copy link

It adds CORS headers in every response

@junihh
Copy link

junihh commented Jul 5, 2020

Great for me. Now I don't need to add "Flask-CORS" package. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment