.\" Man page generated from reStructuredText. . . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .TH "FLASK-CORS" "1" "Jan 22, 2023" "3.0.10" "Flask-Cors" .SH NAME flask-cors \- Flask-Cors Documentation .sp A Flask extension for handling Cross Origin Resource Sharing (CORS), making cross\-origin AJAX possible. .sp This package has a simple philosophy: when you want to enable CORS, you wish to enable it for all use cases on a domain. This means no mucking around with different allowed headers, methods, etc. .sp By default, submission of cookies across domains is disabled due to the security implications. Please see the documentation for how to enable credential\(aqed requests, and please make sure you add some sort of \fI\%CSRF\fP protection before doing so! .SH INSTALLATION .sp Install the extension with using pip, or easy_install. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ pip install \-U flask\-cors .ft P .fi .UNINDENT .UNINDENT .SH USAGE .sp This package exposes a Flask extension which by default enables CORS support on all routes, for all origins and methods. It allows parameterization of all CORS headers on a per\-resource level. The package also contains a decorator, for those who prefer this approach. .SS Simple Usage .sp In the simplest case, initialize the Flask\-Cors extension with default arguments in order to allow CORS for all domains on all routes. See the full list of options in the \fI\%documentation\fP\&. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C from flask import Flask from flask_cors import CORS app = Flask(__name__) CORS(app) @app.route(\(dq/\(dq) def helloWorld(): return \(dqHello, cross\-origin\-world!\(dq .ft P .fi .UNINDENT .UNINDENT .SS Resource specific CORS .sp Alternatively, you can specify CORS options on a resource and origin level of granularity by passing a dictionary as the \fIresources\fP option, mapping paths to a set of options. See the full list of options in the \fI\%documentation\fP\&. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C app = Flask(__name__) cors = CORS(app, resources={r\(dq/api/*\(dq: {\(dqorigins\(dq: \(dq*\(dq}}) @app.route(\(dq/api/v1/users\(dq) def list_users(): return \(dquser example\(dq .ft P .fi .UNINDENT .UNINDENT .SS Route specific CORS via decorator .sp This extension also exposes a simple decorator to decorate flask routes with. Simply add \fB@cross_origin()\fP below a call to Flask\(aqs \fB@app.route(..)\fP to allow CORS on a given route. See the full list of options in the \fI\%decorator documentation\fP\&. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C @app.route(\(dq/\(dq) @cross_origin() def helloWorld(): return \(dqHello, cross\-origin\-world!\(dq .ft P .fi .UNINDENT .UNINDENT .SH DOCUMENTATION .sp For a full list of options, please see the full \fI\%documentation\fP .SH TROUBLESHOOTING .sp If things aren\(aqt working as you expect, enable logging to help understand what is going on under the hood, and why. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C logging.getLogger(\(aqflask_cors\(aq).level = logging.DEBUG .ft P .fi .UNINDENT .UNINDENT .SH TESTS .sp A simple set of tests is included in \fBtest/\fP\&. To run, install nose, and simply invoke \fBnosetests\fP or \fBpython setup.py test\fP to exercise the tests. .SH CONTRIBUTING .sp Questions, comments or improvements? Please create an issue on \fI\%Github\fP, tweet at \fI\%@corydolphin\fP or send me an email. I do my best to include every contribution proposed in any way that I can. .SH CREDITS .sp This Flask extension is based upon the \fI\%Decorator for the HTTP Access Control\fP written by Armin Ronacher. .SS Configuration .sp Flask\-CORS can be configured at four different locations. Configuration values are determined in the following order: .INDENT 0.0 .INDENT 3.5 .INDENT 0.0 .IP 1. 3 Resource level settings (e.g when passed as a dictionary) .IP 2. 3 Keyword argument settings .IP 3. 3 App level configuration settings (e.g. CORS_*) .IP 4. 3 \fI\%Default settings\fP .UNINDENT .UNINDENT .UNINDENT .sp See \fI\%below\fP for more information. .SS Configuration options .sp Configuration options are consistently named across the various \fI\%locations\fP where they can be set. A configuration option called \fIexample\fP can be set with the resource dictionary key \fIexample\fP, as the keyword argument \fIexample\fP or as the Flask app configuration key \fICORS_EXAMPLE\fP\&. .sp The configuration options recognised by Flask\-CORS are: .INDENT 0.0 .TP .B CORS_ALLOW_HEADERS (\fI\%List\fP or \fI\%str\fP) Headers to accept from the client. Headers in the \fI\%Access\-Control\-Request\-Headers\fP request header (usually part of the preflight OPTIONS request) matching headers in this list will be included in the \fI\%Access\-Control\-Allow\-Headers\fP response header. .TP .B CORS_ALWAYS_SEND (\fI\%bool\fP) Usually, if a request doesn\(aqt include an \fI\%Origin\fP header, the client did not request CORS. This means we can ignore this request. .sp However, if this is true, a most\-likely\-to\-be\-correct value is still set. .TP .B CORS_AUTOMATIC_OPTIONS (\fI\%bool\fP) Only applies to the \fI\%flask_cors.cross_origin()\fP decorator. If True, Flask\-CORS will override Flaskā€™s default OPTIONS handling to return CORS headers for OPTIONS requests. .TP .B CORS_EXPOSE_HEADERS (\fI\%List\fP or \fI\%str\fP) The CORS spec requires the server to give explicit permissions for the client to read headers in CORS responses (via the \fI\%Access\-Control\-Expose\-Headers\fP header). This specifies the headers to include in this header. .TP .B CORS_INTERCEPT_EXCEPTIONS (\fI\%bool\fP) Whether to deal with Flask exception handlers or leave them alone (with respect to CORS headers). .TP .B CORS_MAX_AGE (\fI\%timedelta\fP, \fI\%int\fP or \fI\%str\fP) The maximum time for which this CORS request may be cached. This value is set as the \fI\%Access\-Control\-Max\-Age\fP header. .TP .B CORS_METHODS (\fI\%List\fP or \fI\%str\fP) The method(s) which the allowed origins are allowed to access. These are included in the \fI\%Access\-Control\-Allow\-Methods\fP response headers to the preflight OPTIONS requests. .UNINDENT .INDENT 0.0 .TP .B CORS_ORIGINS (\fI\%List\fP, \fI\%str\fP or \fBre.Pattern\fP) The origin(s) to allow requests from. An origin configured here that matches the value of the \fI\%Origin\fP header in a preflight OPTIONS request is returned as the value of the \fI\%Access\-Control\-Allow\-Origin\fP response header. .TP .B CORS_RESOURCES (\fI\%Dict\fP, \fI\%List\fP or \fI\%str\fP) The series of regular expression and (optionally) associated CORS options to be applied to the given resource path. .sp If the value is a dictionary, it\(aqs keys must be regular expressions matching resources, and the values must be another dictionary of configuration options, as described in this section. .sp If the argument is a list, it is expected to be a list of regular expressions matching resources for which the app\-wide configured options are applied. .sp If the argument is a string, it is expected to be a regular expression matching resources for which the app\-wide configured options are applied. .TP .B CORS_SEND_WILDCARD (\fI\%bool\fP) If \fI\%CORS_ORIGINS\fP is \fB\(dq*\(dq\fP and this is true, then the \fI\%Access\-Control\-Allow\-Origin\fP response header\(aqs value with be \fB\(dq*\(dq\fP as well, instead of the value of the \fI\%Origin\fP request header. .TP .B CORS_SUPPORTS_CREDENTIALS (\fI\%bool\fP) Allows users to make authenticated requests. If true, injects the \fI\%Access\-Control\-Allow\-Credentials\fP header in responses. This allows cookies and credentials to be submitted across domains. .INDENT 7.0 .TP .B note This option cannot be used in conjunction with a \(dq*\(dq origin .UNINDENT .TP .B CORS_VARY_HEADER: (\fI\%bool\fP) Enables or disables the injection of the \fI\%Vary\fP response header is set to \fBOrigin\fP\&. This informs clients that our CORS headers are dynamic and cannot be cached. .UNINDENT .SS Default values .INDENT 0.0 .IP \(bu 2 CORS_ALLOW_HEADERS: \(dq*\(dq .IP \(bu 2 CORS_ALWAYS_SEND: True .IP \(bu 2 CORS_AUTOMATIC_OPTIONS: True .IP \(bu 2 CORS_EXPOSE_HEADERS: None .IP \(bu 2 CORS_INTERCEPT_EXCEPTIONS: True .IP \(bu 2 CORS_MAX_AGE: None .IP \(bu 2 CORS_METHODS: [\(dq\fI\%GET\fP\(dq, \(dq\fI\%HEAD\fP\(dq, \(dq\fI\%POST\fP\(dq, \(dq\fI\%OPTIONS\fP\(dq, \(dq\fI\%PUT\fP\(dq, \(dq\fI\%PATCH\fP\(dq, \(dq\fI\%DELETE\fP\(dq] .IP \(bu 2 CORS_ORIGINS: \(dq*\(dq .IP \(bu 2 CORS_RESOURCES: r\(dq/*\(dq .IP \(bu 2 CORS_SEND_WILDCARD: False .IP \(bu 2 CORS_SUPPORTS_CREDENTIALS: False .IP \(bu 2 CORS_VARY_HEADER: True .UNINDENT .SS Locations .SS Resource level settings .sp You can specify CORS options on a resource level of granularity by passing a dictionary as the \fIresources\fP keyword argument when instantiating the \fI\%flask_cors.CORS\fP object (or when calling \fBinit_app\fP on it), mapping paths to a set of options. .SS Keyword argument settings .sp For options matching all resources, it\(aqs also possible to simply set the configuration options using keyword arguments when instantiating the \fI\%flask_cors.CORS\fP object (or when calling \fBinit_app\fP on it). .SS App level configuration settings .sp It\(aqs good practice to keep your application configuration settings in one place. This is also possible with Flask\-CORS using the same configuration options in the Flas application\(aqs config object. .SS Default settings .sp Finally, every setting has a \fI\%default value\fP as well. .SS API Docs .sp This package exposes a Flask extension which by default enables CORS support on all routes, for all origins and methods. It allows parameterization of all CORS headers on a per\-resource level. The package also contains a decorator, for those who prefer this approach. .SS Extension .sp This is the suggested approach to enabling CORS. The default configuration will work well for most use cases. .INDENT 0.0 .TP .B class flask_cors.CORS(app=None, **kwargs) Initializes Cross Origin Resource sharing for the application. The arguments are identical to \fI\%cross_origin()\fP, with the addition of a \fIresources\fP parameter. The resources parameter defines a series of regular expressions for resource paths to match and optionally, the associated options to be applied to the particular resource. These options are identical to the arguments to \fI\%cross_origin()\fP\&. .sp The settings for CORS are determined in the following order .INDENT 7.0 .IP 1. 3 Resource level settings (e.g when passed as a dictionary) .IP 2. 3 Keyword argument settings .IP 3. 3 App level configuration settings (e.g. CORS_*) .IP 4. 3 Default settings .UNINDENT .sp Note: as it is possible for multiple regular expressions to match a resource path, the regular expressions are first sorted by length, from longest to shortest, in order to attempt to match the most specific regular expression. This allows the definition of a number of specific resource options, with a wildcard fallback for all other resources. .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBresources\fP (\fI\%dict\fP\fI, \fP\fIiterable\fP\fI or \fP\fIstring\fP) \-\- .sp The series of regular expression and (optionally) associated CORS options to be applied to the given resource path. .sp If the argument is a dictionary, it\(aqs keys must be regular expressions, and the values must be a dictionary of kwargs, identical to the kwargs of this function. .sp If the argument is a list, it is expected to be a list of regular expressions, for which the app\-wide configured options are applied. .sp If the argument is a string, it is expected to be a regular expression for which the app\-wide configured options are applied. .sp Default : Match all and apply app\-level configuration .IP \(bu 2 \fBorigins\fP (\fI\%list\fP\fI, \fP\fIstring\fP\fI or \fP\fIregex\fP) \-\- .sp The origin, or list of origins to allow requests from. The origin(s) may be regular expressions, case\-sensitive strings, or else an asterisk .sp Default : \(aq*\(aq .IP \(bu 2 \fBmethods\fP (\fI\%list\fP\fI or \fP\fIstring\fP) \-\- .sp The method or list of methods which the allowed origins are allowed to access for non\-simple requests. .sp Default : [GET, HEAD, POST, OPTIONS, PUT, PATCH, DELETE] .IP \(bu 2 \fBexpose_headers\fP (\fI\%list\fP\fI or \fP\fIstring\fP) \-\- .sp The header or list which are safe to expose to the API of a CORS API specification. .sp Default : None .IP \(bu 2 \fBallow_headers\fP (\fI\%list\fP\fI, \fP\fIstring\fP\fI or \fP\fIregex\fP) \-\- .sp The header or list of header field names which can be used when this resource is accessed by allowed origins. The header(s) may be regular expressions, case\-sensitive strings, or else an asterisk. .sp Default : \(aq*\(aq, allow all headers .IP \(bu 2 \fBsupports_credentials\fP (\fI\%bool\fP) \-\- .sp Allows users to make authenticated requests. If true, injects the \fIAccess\-Control\-Allow\-Credentials\fP header in responses. This allows cookies and credentials to be submitted across domains. .INDENT 2.0 .TP .B note This option cannot be used in conjunction with a \(aq*\(aq origin .UNINDENT .sp Default : False .IP \(bu 2 \fBmax_age\fP (\fItimedelta\fP\fI, \fP\fIinteger\fP\fI, \fP\fIstring\fP\fI or \fP\fINone\fP) \-\- .sp The maximum time for which this CORS request maybe cached. This value is set as the \fIAccess\-Control\-Max\-Age\fP header. .sp Default : None .IP \(bu 2 \fBsend_wildcard\fP (\fI\%bool\fP) \-\- .sp If True, and the origins parameter is \fI*\fP, a wildcard \fIAccess\-Control\-Allow\-Origin\fP header is sent, rather than the request\(aqs \fIOrigin\fP header. .sp Default : False .IP \(bu 2 \fBvary_header\fP (\fI\%bool\fP) \-\- .sp If True, the header Vary: Origin will be returned as per the W3 implementation guidelines. .sp Setting this header when the \fIAccess\-Control\-Allow\-Origin\fP is dynamically generated (e.g. when there is more than one allowed origin, and an Origin than \(aq*\(aq is returned) informs CDNs and other caches that the CORS headers are dynamic, and cannot be cached. .sp If False, the Vary header will never be injected or altered. .sp Default : True .UNINDENT .UNINDENT .UNINDENT .SS Decorator .sp If the \fICORS\fP extension does not satisfy your needs, you may find the decorator useful. It shares options with the extension, and should be simple to use. .INDENT 0.0 .TP .B flask_cors.cross_origin(*args, **kwargs) This function is the decorator which is used to wrap a Flask route with. In the simplest case, simply use the default parameters to allow all origins in what is the most permissive configuration. If this method modifies state or performs authentication which may be brute\-forced, you should add some degree of protection, such as Cross Site Forgery Request protection. .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBorigins\fP (\fI\%list\fP\fI, \fP\fIstring\fP\fI or \fP\fIregex\fP) \-\- .sp The origin, or list of origins to allow requests from. The origin(s) may be regular expressions, case\-sensitive strings, or else an asterisk .sp Default : \(aq*\(aq .IP \(bu 2 \fBmethods\fP (\fI\%list\fP\fI or \fP\fIstring\fP) \-\- .sp The method or list of methods which the allowed origins are allowed to access for non\-simple requests. .sp Default : [GET, HEAD, POST, OPTIONS, PUT, PATCH, DELETE] .IP \(bu 2 \fBexpose_headers\fP (\fI\%list\fP\fI or \fP\fIstring\fP) \-\- .sp The header or list which are safe to expose to the API of a CORS API specification. .sp Default : None .IP \(bu 2 \fBallow_headers\fP (\fI\%list\fP\fI, \fP\fIstring\fP\fI or \fP\fIregex\fP) \-\- .sp The header or list of header field names which can be used when this resource is accessed by allowed origins. The header(s) may be regular expressions, case\-sensitive strings, or else an asterisk. .sp Default : \(aq*\(aq, allow all headers .IP \(bu 2 \fBsupports_credentials\fP (\fI\%bool\fP) \-\- .sp Allows users to make authenticated requests. If true, injects the \fIAccess\-Control\-Allow\-Credentials\fP header in responses. This allows cookies and credentials to be submitted across domains. .INDENT 2.0 .TP .B note This option cannot be used in conjunction with a \(aq*\(aq origin .UNINDENT .sp Default : False .IP \(bu 2 \fBmax_age\fP (\fItimedelta\fP\fI, \fP\fIinteger\fP\fI, \fP\fIstring\fP\fI or \fP\fINone\fP) \-\- .sp The maximum time for which this CORS request maybe cached. This value is set as the \fIAccess\-Control\-Max\-Age\fP header. .sp Default : None .IP \(bu 2 \fBsend_wildcard\fP (\fI\%bool\fP) \-\- .sp If True, and the origins parameter is \fI*\fP, a wildcard \fIAccess\-Control\-Allow\-Origin\fP header is sent, rather than the request\(aqs \fIOrigin\fP header. .sp Default : False .IP \(bu 2 \fBvary_header\fP (\fI\%bool\fP) \-\- .sp If True, the header Vary: Origin will be returned as per the W3 implementation guidelines. .sp Setting this header when the \fIAccess\-Control\-Allow\-Origin\fP is dynamically generated (e.g. when there is more than one allowed origin, and an Origin than \(aq*\(aq is returned) informs CDNs and other caches that the CORS headers are dynamic, and cannot be cached. .sp If False, the Vary header will never be injected or altered. .sp Default : True .IP \(bu 2 \fBautomatic_options\fP (\fI\%bool\fP) \-\- .sp Only applies to the \fIcross_origin\fP decorator. If True, Flask\-CORS will override Flask\(aqs default OPTIONS handling to return CORS headers for OPTIONS requests. .sp Default : True .UNINDENT .UNINDENT .UNINDENT .SS Using \fICORS\fP with cookies .sp By default, Flask\-CORS does not allow cookies to be submitted across sites, since it has potential security implications. If you wish to enable cross\-site cookies, you may wish to add some sort of \fI\%CSRF\fP protection to keep you and your users safe. .sp To allow cookies or authenticated requests to be made cross origins, simply set the \fIsupports_credentials\fP option to \fITrue\fP\&. E.g. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C from flask import Flask, session from flask_cors import CORS app = Flask(__name__) CORS(app, supports_credentials=True) @app.route(\(dq/\(dq) def helloWorld(): return \(dqHello, %s\(dq % session[\(aqusername\(aq] .ft P .fi .UNINDENT .UNINDENT .sp The above code enables Flask backend to accept cookies to be submitted from cross origin sites. But if you are sending Xhr requests (ajax calls) to a cross\-origin server, by default chrome or any modern browser won\(aqt send cookies and session with the request. You should use \fBwithCredentials = True\fP while sending Xhr request to enable that. You should keep in mind about the necessary security concerns. Related MDN doc: \fI\%https://developer.mozilla.org/en\-US/docs/Web/API/XMLHttpRequest/withCredentials\fP .SS Using \fICORS\fP with Blueprints .sp Flask\-CORS supports blueprints out of the box. Simply pass a \fIblueprint\fP instance to the CORS extension, and everything will just work. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C api_v1 = Blueprint(\(aqAPI_v1\(aq, __name__) CORS(api_v1) # enable CORS on the API_v1 blue print @api_v1.route(\(dq/api/v1/users/\(dq) def list_users(): \(aq\(aq\(aq Since the path matches the regular expression r\(aq/api/*\(aq, this resource automatically has CORS headers set. The expected result is as follows: $ curl \-\-include \-X GET http://127.0.0.1:5000/api/v1/users/ \e \-\-header Origin:www.examplesite.com HTTP/1.0 200 OK Access\-Control\-Allow\-Headers: Content\-Type Access\-Control\-Allow\-Origin: * Content\-Length: 21 Content\-Type: application/json Date: Sat, 09 Aug 2014 00:26:41 GMT Server: Werkzeug/0.9.4 Python/2.7.8 { \(dqsuccess\(dq: true } \(aq\(aq\(aq return jsonify(user=\(dqjoe\(dq) @api_v1.route(\(dq/api/v1/users/create\(dq, methods=[\(aqPOST\(aq]) def create_user(): \(aq\(aq\(aq Since the path matches the regular expression r\(aq/api/*\(aq, this resource automatically has CORS headers set. Browsers will first make a preflight request to verify that the resource allows cross\-origin POSTs with a JSON Content\-Type, which can be simulated as: $ curl \-\-include \-X OPTIONS http://127.0.0.1:5000/api/v1/users/create \e \-\-header Access\-Control\-Request\-Method:POST \e \-\-header Access\-Control\-Request\-Headers:Content\-Type \e \-\-header Origin:www.examplesite.com >> HTTP/1.0 200 OK Content\-Type: text/html; charset=utf\-8 Allow: POST, OPTIONS Access\-Control\-Allow\-Origin: * Access\-Control\-Allow\-Headers: Content\-Type Access\-Control\-Allow\-Methods: DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT Content\-Length: 0 Server: Werkzeug/0.9.6 Python/2.7.9 Date: Sat, 31 Jan 2015 22:25:22 GMT $ curl \-\-include \-X POST http://127.0.0.1:5000/api/v1/users/create \e \-\-header Content\-Type:application/json \e \-\-header Origin:www.examplesite.com >> HTTP/1.0 200 OK Content\-Type: application/json Content\-Length: 21 Access\-Control\-Allow\-Origin: * Server: Werkzeug/0.9.6 Python/2.7.9 Date: Sat, 31 Jan 2015 22:25:04 GMT { \(dqsuccess\(dq: true } \(aq\(aq\(aq return jsonify(success=True) public_routes = Blueprint(\(aqpublic\(aq, __name__) @public_routes.route(\(dq/\(dq) def helloWorld(): \(aq\(aq\(aq Since the path \(aq/\(aq does not match the regular expression r\(aq/api/*\(aq, this route does not have CORS headers set. \(aq\(aq\(aq return \(aq\(aq\(aq

Hello CORS!

Read about my spec at the W3 Or, checkout my documentation on Github\(aq\(aq\(aq logging.basicConfig(level=logging.INFO) app = Flask(\(aqFlaskCorsBlueprintBasedExample\(aq) app.register_blueprint(api_v1) app.register_blueprint(public_routes) if __name__ == \(dq__main__\(dq: app.run(debug=True) .ft P .fi .UNINDENT .UNINDENT .SS Examples .SS Using the \fICORS\fP extension .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # One of the simplest configurations. Exposes all resources matching /api/* to # CORS and allows the Content\-Type header, which is necessary to POST JSON # cross origin. CORS(app, resources=r\(aq/api/*\(aq) @app.route(\(dq/\(dq) def helloWorld(): \(dq\(dq\(dq Since the path \(aq/\(aq does not match the regular expression r\(aq/api/*\(aq, this route does not have CORS headers set. \(dq\(dq\(dq return \(aq\(aq\(aq

Hello CORS!

End to end editable example with jquery!

JS Bin on jsbin.com \(aq\(aq\(aq @app.route(\(dq/api/v1/users/\(dq) def list_users(): \(dq\(dq\(dq Since the path matches the regular expression r\(aq/api/*\(aq, this resource automatically has CORS headers set. The expected result is as follows: $ curl \-\-include \-X GET http://127.0.0.1:5000/api/v1/users/ \e \-\-header Origin:www.examplesite.com HTTP/1.0 200 OK Access\-Control\-Allow\-Headers: Content\-Type Access\-Control\-Allow\-Origin: * Content\-Length: 21 Content\-Type: application/json Date: Sat, 09 Aug 2014 00:26:41 GMT Server: Werkzeug/0.9.4 Python/2.7.8 { \(dqsuccess\(dq: true } \(dq\(dq\(dq return jsonify(user=\(dqjoe\(dq) @app.route(\(dq/api/v1/users/create\(dq, methods=[\(aqPOST\(aq]) def create_user(): \(dq\(dq\(dq Since the path matches the regular expression r\(aq/api/*\(aq, this resource automatically has CORS headers set. Browsers will first make a preflight request to verify that the resource allows cross\-origin POSTs with a JSON Content\-Type, which can be simulated as: $ curl \-\-include \-X OPTIONS http://127.0.0.1:5000/api/v1/users/create \e \-\-header Access\-Control\-Request\-Method:POST \e \-\-header Access\-Control\-Request\-Headers:Content\-Type \e \-\-header Origin:www.examplesite.com >> HTTP/1.0 200 OK Content\-Type: text/html; charset=utf\-8 Allow: POST, OPTIONS Access\-Control\-Allow\-Origin: * Access\-Control\-Allow\-Headers: Content\-Type Access\-Control\-Allow\-Methods: DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT Content\-Length: 0 Server: Werkzeug/0.9.6 Python/2.7.9 Date: Sat, 31 Jan 2015 22:25:22 GMT $ curl \-\-include \-X POST http://127.0.0.1:5000/api/v1/users/create \e \-\-header Content\-Type:application/json \e \-\-header Origin:www.examplesite.com >> HTTP/1.0 200 OK Content\-Type: application/json Content\-Length: 21 Access\-Control\-Allow\-Origin: * Server: Werkzeug/0.9.6 Python/2.7.9 Date: Sat, 31 Jan 2015 22:25:04 GMT { \(dqsuccess\(dq: true } \(dq\(dq\(dq return jsonify(success=True) @app.route(\(dq/api/exception\(dq) def get_exception(): \(dq\(dq\(dq Since the path matches the regular expression r\(aq/api/*\(aq, this resource automatically has CORS headers set. Browsers will first make a preflight request to verify that the resource allows cross\-origin POSTs with a JSON Content\-Type, which can be simulated as: $ curl \-\-include \-X OPTIONS http://127.0.0.1:5000/api/exception \e \-\-header Access\-Control\-Request\-Method:POST \e \-\-header Access\-Control\-Request\-Headers:Content\-Type \e \-\-header Origin:www.examplesite.com >> HTTP/1.0 200 OK Content\-Type: text/html; charset=utf\-8 Allow: POST, OPTIONS Access\-Control\-Allow\-Origin: * Access\-Control\-Allow\-Headers: Content\-Type Access\-Control\-Allow\-Methods: DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT Content\-Length: 0 Server: Werkzeug/0.9.6 Python/2.7.9 Date: Sat, 31 Jan 2015 22:25:22 GMT \(dq\(dq\(dq raise Exception(\(dqexample\(dq) @app.errorhandler(500) def server_error(e): logging.exception(\(aqAn error occurred during a request. %s\(aq, e) return \(dqAn internal error occured\(dq, 500 if __name__ == \(dq__main__\(dq: app.run(debug=True) .ft P .fi .UNINDENT .UNINDENT .SS Using the \fIcross_origins\fP decorator .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C @app.route(\(dq/\(dq, methods=[\(aqGET\(aq]) @cross_origin() def helloWorld(): \(aq\(aq\(aq This view has CORS enabled for all domains, representing the simplest configuration of view\-based decoration. The expected result is as follows: $ curl \-\-include \-X GET http://127.0.0.1:5000/ \e \-\-header Origin:www.examplesite.com >> HTTP/1.0 200 OK Content\-Type: text/html; charset=utf\-8 Content\-Length: 184 Access\-Control\-Allow\-Origin: * Server: Werkzeug/0.9.6 Python/2.7.9 Date: Sat, 31 Jan 2015 22:29:56 GMT

Hello CORS!

Read about my spec at the W3 Or, checkout my documentation on Github \(aq\(aq\(aq return \(aq\(aq\(aq

Hello CORS!

Read about my spec at the W3 Or, checkout my documentation on Github\(aq\(aq\(aq @app.route(\(dq/api/v1/users/create\(dq, methods=[\(aqGET\(aq, \(aqPOST\(aq]) @cross_origin(allow_headers=[\(aqContent\-Type\(aq]) def cross_origin_json_post(): \(aq\(aq\(aq This view has CORS enabled for all domains, and allows browsers to send the Content\-Type header, allowing cross domain AJAX POST requests. Browsers will first make a preflight request to verify that the resource allows cross\-origin POSTs with a JSON Content\-Type, which can be simulated as: $ curl \-\-include \-X OPTIONS http://127.0.0.1:5000/api/v1/users/create \e \-\-header Access\-Control\-Request\-Method:POST \e \-\-header Access\-Control\-Request\-Headers:Content\-Type \e \-\-header Origin:www.examplesite.com >> HTTP/1.0 200 OK Content\-Type: text/html; charset=utf\-8 Allow: POST, OPTIONS Access\-Control\-Allow\-Origin: * Access\-Control\-Allow\-Headers: Content\-Type Access\-Control\-Allow\-Methods: DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT Content\-Length: 0 Server: Werkzeug/0.9.6 Python/2.7.9 Date: Sat, 31 Jan 2015 22:25:22 GMT $ curl \-\-include \-X POST http://127.0.0.1:5000/api/v1/users/create \e \-\-header Content\-Type:application/json \e \-\-header Origin:www.examplesite.com >> HTTP/1.0 200 OK Content\-Type: application/json Content\-Length: 21 Access\-Control\-Allow\-Origin: * Server: Werkzeug/0.9.6 Python/2.7.9 Date: Sat, 31 Jan 2015 22:25:04 GMT { \(dqsuccess\(dq: true } \(aq\(aq\(aq return jsonify(success=True) if __name__ == \(dq__main__\(dq: app.run(debug=True) .ft P .fi .UNINDENT .UNINDENT .SH AUTHOR Cory Dolphin .SH COPYRIGHT 2023, Cory Dolphin .\" Generated by docutils manpage writer. .