Flask-Cors not working while using url_prefix in blueprints
See original GitHub issueI was using flask-cors
for my flask application which failed to enable CORS for blueprints where blueprints were registered along with url_prefix
as their property. However, when I removed url_prefix
and manually added prefix in blueprints, CORS seemed to be working fine.
Example
This code works properly
from flask import Flask, make_response
from flask_cors import CORS
from tracer.blueprints.auth import app as auth_blueprint
from tracer.blueprints.url import app as url_blueprint
app = Flask(__name__)
CORS(app)
app.register_blueprint(auth_blueprint)
app.register_blueprint(url_blueprint)
if __name__ == '__main__':
app.run(host='0.0.0.0')
This code doesn’t allow CORS
.
.
app.register_blueprint(auth_blueprint, url_prefix='/auth')
app.register_blueprint(url_blueprint, url_prefix='/url')
.
.
Issue Analytics
- State:
- Created 3 years ago
- Comments:11
Top Results From Across the Web
Unable to run cors on flask app with blueprints - Stack Overflow
I am unable to get cors enabled on my app inside my flask app using the flask_cors package after I started using blueprints...
Read more >Blueprints and Views — Flask Documentation (2.2.x)
A Blueprint is a way to organize a group of related views and other code. Rather than registering views and other code directly...
Read more >Flask-Security Documentation
Specifies the URL prefix for the Flask-Security blueprint. ... Default: a list of known schemes not working with double hashing ...
Read more >(PDF) Explore Flask Documentation - Read the DocsExplore ...
I finally released the book, after spending almost a year working on it. ... I'm not using it myself currently, and I don't...
Read more >python-flask-security-too(1) - Arch manual pages
At top of file from flask_mail import Mail # After 'Create app' ... Default: a list of known schemes not working with double...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
yup, we can close it, thanks to everyone for your help and suggestions, However, the @gianlukk994 solution worked fine for me. thanks, @gianlukk994.
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. Upon closing inspection I realized that the ‘bad’ urls where returning 308s. After looking at the Location header I realized that it was failing because my blueprint routes for those failing endpoints had trailing slashes in them. Once I took them out it all worked fine. Why Axios or Chrome can’t just process the redirect instead of failing with a misleading ‘CORS’ issue is beyond me.
Anyway, here is an example of what I mean: