Last active
October 7, 2015 14:34
-
-
Save mushfiq/fa0ae599fc4b44ddba8d to your computer and use it in GitHub Desktop.
python decorator for bottle framework to enable CORS to an endpoint.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bottle import Bottle,response | |
def allow_cors(func): | |
""" this is a decorator which enable CORS for specified endpoint """ | |
def wrapper(*args, **kwargs): | |
response.headers['Access-Control-Allow-Origin'] = 'example.com' # * in case you want to be accessed via any website | |
return func(*args, **kwargs) | |
return wrapper | |
#example usages in an API endpoint | |
app = Bottle() | |
@app.route("/endpoint/:id") | |
@allow_cors | |
def get_cakes_by_id(): | |
# loading cakes by ID | |
return {"cakes": cakes} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment