question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Flask-Cors not working while using url_prefix in blueprints

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:11

github_iconTop GitHub Comments

3reactions
aalokrmbcommented, Oct 14, 2021

This issue has to close 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.

3reactions
PhilipReadcommented, Jul 30, 2021

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:

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():
...

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found