Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple way to allow all CORS / OPTIONS requests? #1220

Closed
ghost opened this issue Feb 5, 2018 · 5 comments
Closed

Simple way to allow all CORS / OPTIONS requests? #1220

ghost opened this issue Feb 5, 2018 · 5 comments

Comments

@ghost
Copy link

ghost commented Feb 5, 2018

I'm wanting to open the door wide to all CORS requests to my application.

I can see there is an additional Falcon/CORS middleware project but I'm hoping there is some simpler way to just open the door wide with a few lines of code and no need to worry about understanding and implementing the middleware and the various options and fine tuning that it provides.

Any suggestions for how to do this?

@ghost
Copy link
Author

ghost commented Feb 6, 2018

Answering my own question:

import falcon
from falcon.http_status import HTTPStatus

class HandleCORS(object):
    def process_request(self, req, resp):
        resp.set_header('Access-Control-Allow-Origin', '*')
        resp.set_header('Access-Control-Allow-Methods', '*')
        resp.set_header('Access-Control-Allow-Headers', '*')
        resp.set_header('Access-Control-Max-Age', 1728000)  # 20 days
        if req.method == 'OPTIONS':
            raise HTTPStatus(falcon.HTTP_200, body='\n')

wsgi_app = api = falcon.API(middleware=[HandleCORS() ])

@vytas7
Copy link
Member

vytas7 commented Feb 7, 2018

It seems you have coined a solution that fits your needs 😈

Your question is otherwise actually answered in the FAQ [1], and the answer is really fairly similar to yours. Note the suggestion handles 'OPTIONS' in a slightly different way (by telling which methods are supported instead of saying "all methods"), but yours would do too.

  1. https://falcon.readthedocs.io/en/stable/user/faq.html#how-do-i-implement-cors-with-falcon

@vytas7
Copy link
Member

vytas7 commented Feb 7, 2018

Also note that a simple to way to add unsophisticated permissive CORS is under consideration to be included in Falcon 2.0: #1194

@kgriffs kgriffs closed this as completed Apr 3, 2018
heilaaks added a commit to heilaaks/snippy that referenced this issue Jun 9, 2018
The OPTIONS method is now supported for each route. This method
can be used to check allowed methods.

CORS is not yet supported. Something along [1], [2] or could
be used.

[1] https://stackoverflow.com/a/45183343
[2] falconry/falcon#1220
[3] https://github.com/lwcolton/falcon-cors

Signed-off-by: Heikki Laaksonen <laaksonen.heikki.j@gmail.com>
@MockArch
Copy link

DeprecatedWarning: Call to deprecated function init(...). API class may be removed in a future release, use falcon.App instead.

use :

`class HandleCORS(object):
def process_request(self, req, resp):
resp.set_header('Access-Control-Allow-Origin', '')
resp.set_header('Access-Control-Allow-Methods', '
')
resp.set_header('Access-Control-Allow-Headers', '*')
resp.set_header('Access-Control-Max-Age', 1728000) # 20 days
if req.method == 'OPTIONS':
raise HTTPStatus(falcon.HTTP_200, body='\n')

app = falcon.App(middleware=[HandleCORS() ])`

@vytas7
Copy link
Member

vytas7 commented Feb 12, 2022

Thanks for the tip, @MockArch 👍

Note though that as of Falcon 3.0+, the CORS functionality is now included in the framework.
The simplest way of adding a permissive CORS policy is now:

import falcon

# Enable a simple CORS policy for all responses
app = falcon.App(cors_enable=True)

(You can also address some more complex use cases by adding an instance of CORSMiddleware to your app.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants