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

CORS Support #28

Open
lkskstlr opened this issue Aug 2, 2023 · 3 comments
Open

CORS Support #28

lkskstlr opened this issue Aug 2, 2023 · 3 comments

Comments

@lkskstlr
Copy link

lkskstlr commented Aug 2, 2023

Hi all,

I really like your service, but it seems that CORS is missing (or is there a recommended workaround)? I think it shouldn't be hard to add it, given that you have a thin wrapper around Flask. Is this on your timeline and if so, what would be the approximate timing?

Thanks
Lukas

@jaxball
Copy link

jaxball commented Aug 20, 2023

@lkskstlr I was also trying to find this option. Since CORS is likely intended for local development, you can use the following monkey patch to achieve the same effect:

from potassium import Potassium, Request, Response
from flask import Flask

def new_create_flask_app(self):
    flask_app = original_create_flask_app(self)

    @flask_app.after_request
    def add_cors_headers(response):
        response.headers.add('Access-Control-Allow-Origin', 'http://localhost:3000')
        response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
        response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
        return response
    return flask_app

# monkey patch the new method
original_create_flask_app = Potassium._create_flask_app
Potassium._create_flask_app = new_create_flask_app

@wimvanhenden-tool
Copy link

Hi all,

I am having the same issues with Cors. Outside of Postman I can not make any calls to my Banana instance. Not on localhost, not even on a AWS Cloudfront/s3 with https.

@jaxball how exactly does your code tie into creating an app with Potassium like this:
app = Potassium("my_app")

Thank you so much,

@jgentes
Copy link

jgentes commented Oct 29, 2023

Thanks @jaxball that works! Here's an example of the code (note the Flask import isn't required):

from potassium import Potassium, Request, Response

# for CORS support
def new_create_flask_app(self):
    flask_app = original_create_flask_app(self)

    @flask_app.after_request
    def add_cors_headers(response):
        response.headers.add('Access-Control-Allow-Origin', '*')
        response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
        return response
    return flask_app

# monkey patch the new method
original_create_flask_app = Potassium._create_flask_app
Potassium._create_flask_app = new_create_flask_app

app = Potassium("my_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

4 participants