Saturday, 17 August 2013

Flask add parameter to route method in before_request

Flask add parameter to route method in before_request

Lets say I have an API at /api/something. The API requires a definition
for api_key, it looks in the request arguments and the cookies. If it
finds the api_key, I want it to pass the api_key to the route methods, in
this case something.
@app.before_request
def pass_api_key():
api_key = request.args.get('api_key', None)
if api_key is None:
api_key = request.cookies.get('api_key', None)
if api_key is None:
return 'api_key is required'
# add parameter of api_key to something method
@app.route('/api/something')
def something(api_key):
return api_key
Is this possible?
Thanks in advance.

No comments:

Post a Comment