Skip to content

Instantly share code, notes, and snippets.

@isaaguilar
Last active January 17, 2017 08:44
Show Gist options
  • Save isaaguilar/27d754ae29c6ef19c9629c4bf7c341e8 to your computer and use it in GitHub Desktop.
Save isaaguilar/27d754ae29c6ef19c9629c4bf7c341e8 to your computer and use it in GitHub Desktop.
from functools import wraps
def parse_request_data(f):
@wraps(f)
def parse(*args, **kwargs):
json_type = False
for header in request.headers:
if header[0] == 'Content-Type':
if "application/json" in header[1].lower():
json_type = True
data = {}
if json_type:
try:
# load url args are json
for key, value in request.values.iteritems():
try:
value = json.loads(value)
except:
pass
data.update({key: value})
# load the json data params
for key, value in request.json.iteritems():
data.update({key: value})
except: # (werkzeug.exceptions.BadRequest)
pass
else:
for key, value in request.values.iteritems():
data.update({key: value})
return f(data, *args, **kwargs)
return parse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment