Last active
January 17, 2017 08:44
-
-
Save isaaguilar/27d754ae29c6ef19c9629c4bf7c341e8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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