corydolphin / flask-cors

Cross Origin Resource Sharing ( CORS ) support for Flask
https://flask-cors.readthedocs.io/en/latest/index.html
MIT License
889 stars 140 forks source link

CORS with blueprint #169

Closed georgexsh closed 8 years ago

georgexsh commented 8 years ago

with code:

bp = flask.Blueprint('api', __name__, url_prefix='/api')
bp = flask_cors.CORS(bp, max_age=30*86400)

@bp.before_request
def load_user():
    ...

will yield:

E   AttributeError: 'CORS' object has no attribute 'before_request'

my usage is wrong or a bug?

corydolphin commented 8 years ago

Hey @georgexsh so sorry for the delay here.

The issue here is partially our bug (in that the docs are unclear, and perhaps the API).

To fix, simply do not assign the result of calling flask_cors.

E.G

bp = flask.Blueprint('api', __name__, url_prefix='/api')
flask_cors.CORS(bp, max_age=30*86400)

@bp.before_request
def load_user():
    ...
corydolphin commented 8 years ago

I think it is understandable to assume that the extension could be applied in that way, I'll add a note to update it.

georgexsh commented 8 years ago

Thanks very much!