Skip to content

Instantly share code, notes, and snippets.

@mushfiq
Last active October 7, 2015 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mushfiq/fa0ae599fc4b44ddba8d to your computer and use it in GitHub Desktop.
Save mushfiq/fa0ae599fc4b44ddba8d to your computer and use it in GitHub Desktop.
python decorator for bottle framework to enable CORS to an endpoint.
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