-
Notifications
You must be signed in to change notification settings - Fork 140
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
Flask-Cors not working while using url_prefix in blueprints #289
Comments
I was facing the same issue but the error was generated by undefined variables etc. Also in the frontend I set withCredentials to true. Try this: from flask import Flask
from flask_cors import CORS
app = Flask(__name__)
CORS(app, resources={r'/*': origins: '*'}) With credentials from flask import Flask
from flask_cors import CORS
app = Flask(__name__)
CORS(app,
resources={r'/*': origins: '*'},
supports_credentials=True
) |
I am facing the same issue, can anyone please tell me how to resolve it? |
@aalokrmb |
Just thought I'd throw my perspective. I was having the same issue and I could not for the life of me understand why some calls were getting CORS errors. Anyway, here is an example of what I mean: bp = Blueprint('api', __name__, url_prefix = '/api')
# @bp.route('some-resources/', methods = ('GET', 'POST')) # Wrong
@bp.route('some-resources', methods = ('GET', 'POST')) # Right!
def someResources():
... |
Defining url_prefix inside Blueprint constructor works. #app.py
from flask_cors import CORS
CORS(app, resources={r'/*': {'origins': '*'}})
app.register_blueprint(stocks_bp) #blueprint
stocks_bp = Blueprint('stocks_bp', __name__, url_prefix='/stocks') Btw it's strange, hope they fix it. |
I was having similar issue, for me it was the trailing slash # ============== app.py ===============
from flask_cors import CORS
CORS(app, resources={r'/*': {'origins': '*'}})
app.register_blueprint(blue_print_realestate, url_prefix='/api/v1/realestates')
# ========== realestates.py ==============
blueprint_realestate = Blueprint(
name='realestates', import_name=__name__
)
@blueprint_realestate.route("", methods=["POST"]) # <- I had it as "/" instead and was appended apparently
def post_realestate():
# whatever stuff I hope this solves some of the problems In my case, I assume that the trailing slash led the redirect with OPTIONS method and that seemed like what was causing problems. |
This issue has to be closed now. A lot of solutions have come forward. |
yup, we can close it, thanks to everyone for your help and suggestions, However, the @gianlukk994 solution worked fine for me. thanks, @gianlukk994. |
Yes sure. Lot of solutions have come up.
Closing this issue now. |
I am having issues getting flask cors work for me too. I am using from flask_restx import Namespace, Resource
...
ns = Namespace(name='api_v1', path="/api/v1")
@ns.route('/section/<string:url_id>')
class Section(Resource):
def put(self, url_id):
print("Do some Put work")
return None, 201 and I am trying to enable cors on the api end points CORS(app, resources={r"/api/*": {"origins": "*"}}) This is what's trying to call $.ajax({
type: 'PUT',
crossDomain: true,
url: the_url_here,
contentType: 'application/json',
data: {"type": "value"},
}) Am I doing something odd here? Can someone see a fix for it? Have anyone had any luck with flask-cors and flask-restx together? |
There are a lot of solutions on this thread. Try all of them before posting. This issue must be closed now, please. When you allow all origins (*) make sure you have credentials turned off in your request otherwise specify the origins. This is how I always set up a flask server. from flask import Flask
from flask_cors import CORS
from .api import v1_routes
def create_app():
app = Flask(__name__)
# Cross Origin Settings
cors_origin = [
'http://192.168.0.27:4200',
'http://localhost:4200'
]
CORS(
app,
resources={r"/api/*": {"origins": cors_origin}},
supports_credentials=True,
)
with app.app_context():
app.register_blueprint(v1_routes.router, url_prefix='/api/v1')
return app Then in your request headers add {
"withCredentials": true
} If it doesn't work, Let me know. |
I was using
flask-cors
for my flask application which failed to enable CORS for blueprints where blueprints were registered along withurl_prefix
as their property. However, when I removedurl_prefix
and manually added prefix in blueprints, CORS seemed to be working fine.Example
This code works properly
This code doesn't allow CORS
The text was updated successfully, but these errors were encountered: