Skip to main content

A Flask extension adding a decorator for CORS support

Project description

Build Status Latest Version Downloads Supported Python versions License

A Flask extension for handling Cross Origin Resource Sharing (CORS), making cross-origin AJAX possible.

Installation

Install the extension with using pip, or easy_install.

$ pip install flask-cors

Usage

This extension exposes a simple decorator to decorate flask routes with. Simply add @cross_origin() below a call to Flask’s @app.route(..) incanation to accept the default options and allow CORS on a given route.

Simple Usage

@app.route("/")
@cross_origin() # allow all origins all methods.
def helloWorld():
  return "Hello, cross-origin-world!"

Using JSON with Cross Origin

When using JSON cross origin, browsers will issue a pre-flight OPTIONS request for POST requests. In order for browsers to allow POST requests with a JSON content type, you must allow the Content-Type header.

@app.route("/user/create", methods=['GET','POST'])
@cross_origin(headers=['Content-Type']) # Send Access-Control-Allow-Headers
def cross_origin_json_post():
  return jsonify(success=True)

Application-wide settings

Alternatively, you can set any of these options in an app’s config object. Setting these at the application level effectively changes the default value for your application, while still allowing you to override it on a per-resource basis.

The application-wide configuration options are creatively prefixed with CORS_ e.g. * CORS_ORIGINS * CORS_METHODS * CORS_HEADERS * CORS_EXPOSE_HEADERS * CORS_ALWAYS_SEND * CORS_MAX_AGE * CORS_SEND_WILDCARD * CORS_ALWAYS_SEND * CORS_AUTOMATIC_OPTIONS

app.config['CORS_ORIGINS'] = ['https://foo.com', 'http://www.bar.com']
app.config['CORS_HEADERS'] = ['Content-Type']

# Will return CORS headers for origins 'https://foo.com'and 'http://www.bar.com'
# and an Access-Control-Allow-Headers of 'Content-Type'
"""
Will return CORS headers for origins 'https://foo.com'and
'http://www.bar.com' and an Access-Control-Allow-Headers of 'Content-Type'
E.G. Testing with httpie
    ➜  ~  http GET http://127.0.0.1:5000/
    HTTP/1.0 200 OK
    Access-Control-Allow-Headers: Content-Type
    Access-Control-Allow-Origin: https://foo.com, http://www.bar.com
    Content-Length: 26
    Content-Type: text/html; charset=utf-8
    Date: Tue, 22 Jul 2014 23:55:53 GMT
    Server: Werkzeug/0.9.4 Python/2.7.8

    Hello, cross-origin-world!
"""
@app.route("/")
@cross_origin()
def helloWorld():
  return "Hello, cross-origin-world!"


"""
Will return CORS headers for 'google.com' and  Access-Control-Allow-Headers of
'Content-Type'.
E.G. Testing with httpie
     ➜  ~  http GET http://127.0.0.1:5000/special
    HTTP/1.0 200 OK
    Access-Control-Allow-Headers: Content-Type
    Access-Control-Allow-Origin: http://google.com
    Content-Length: 33
    Content-Type: text/html; charset=utf-8
    Date: Tue, 22 Jul 2014 23:55:29 GMT
    Server: Werkzeug/0.9.4 Python/2.7.8

    Hello, google-cross-origin-world!
"""
@app.route("/special")
@cross_origin(origins="http://google.com")
def helloGoogle():
  return "Hello, google-cross-origin-world!"

For a full list of options, please see the full documentation

Tests

A simple set of tests is included in test/. To run, install nose, and simply invoke nosetests or python setup.py test to exercise the tests.

Contributing

Questions, comments or improvements? Please create an issue on Github, tweet at [@wcdolphin](https://twitter.com/wcdolphin) or send me an email.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-Cors-1.6.0.tar.gz (12.2 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

Flask_Cors-1.6.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

Flask_Cors-1.6.0-py2-none-any.whl (8.7 kB view details)

Uploaded Python 2

File details

Details for the file Flask-Cors-1.6.0.tar.gz.

File metadata

  • Download URL: Flask-Cors-1.6.0.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Flask-Cors-1.6.0.tar.gz
Algorithm Hash digest
SHA256 a21f2d1bf5c5d91f56ca1f800f93ee043db308a11ed5f2581ce22f70e0e7aac2
MD5 41e0641e139eeecbace11e3f527bfe57
BLAKE2b-256 4e50661e44717482ed5a2960543e56b9d4d9ddbbbdc1b57c9c22c172fd7e7b8a

See more details on using hashes here.

File details

Details for the file Flask_Cors-1.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for Flask_Cors-1.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7500089e195ca27f97e83d2df1a528dac883d4e2e9e5898785dddeb4aac98df2
MD5 e0d60e03ecc1db151b3b42c30d4f6d00
BLAKE2b-256 be9d0d6127444da7448edd05b280c3d1af8be7a3010a246a18b22abe191e2000

See more details on using hashes here.

File details

Details for the file Flask_Cors-1.6.0-py2-none-any.whl.

File metadata

File hashes

Hashes for Flask_Cors-1.6.0-py2-none-any.whl
Algorithm Hash digest
SHA256 9af158d96453e129b96587868e3a1fd626c6827d1e00383d3568f09952938739
MD5 0eb26ef667f06887bfdcb5a006ce53b1
BLAKE2b-256 655db295661102f3aada177e8fe291a2f3becb1f8fd8c25a1aa604638825b58e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page