def _get_request_data(try_url_params=False): u'''Returns a dictionary, extracted from a request. If there is no data, None or "" is returned. ValueError will be raised if the data is not a JSON-formatted dict. The data is retrieved as a JSON-encoded dictionary from the request body. Or, if the `try_url_params` argument is True and the request is a GET request, then an attempt is made to read the data from the url parameters of the request. try_url_params If try_url_params is False, then the data_dict is read from the request body. If try_url_params is True and the request is a GET request then the data is read from the url parameters. The resulting dict will only be 1 level deep, with the url-param fields being the keys. If a single key has more than one value specified, then the value will be a list of strings, otherwise just a string. ''' def mixed(multi_dict): u'''Return a dict with values being lists if they have more than one item or a string otherwise ''' out = {} for key, value in multi_dict.to_dict(flat=False).iteritems(): out[key] = value[0] if len(value) == 1 else value return out if not try_url_params and request.method == u'GET': raise ValueError(u'Invalid request. Please use POST method ' 'for your request') request_data = {} if request.method in [u'POST', u'PUT'] and request.form: if (len(request.form.values()) == 1 and request.form.values()[0] in [u'1', u'']): try: request_data = json.loads(request.form.keys()[0]) except ValueError as e: raise ValueError(u'Error decoding JSON data. ' 'Error: %r ' 'JSON data extracted from the request: %r' % (e, request_data)) else: request_data = mixed(request.form) elif request.args and try_url_params: request_data = mixed(request.args) elif (request.data and request.data != u'' and request.content_type != u'multipart/form-data'): try: request_data = request.get_json() except BadRequest as e: raise ValueError(u'Error decoding JSON data. ' 'Error: %r ' 'JSON data extracted from the request: %r' % (e, request_data)) if not isinstance(request_data, dict): raise ValueError(u'Request data JSON decoded to %r but ' 'it needs to be a dictionary.' % request_data) if request.method == u'PUT' and not request_data: raise ValueError(u'Invalid request. Please use the POST method for ' 'your request') for field_name, file_ in request.files.iteritems(): request_data[field_name] = file_ log.debug(u'Request data extracted: %r', request_data) return request_data
and request.form.values()[0] in [u'1', u'']): try: request_data = json.loads(request.form.keys()[0]) except ValueError, e: raise ValueError(u'Error decoding JSON data. ' 'Error: %r ' 'JSON data extracted from the request: %r' % (e, request_data)) else: request_data = mixed(request.form) elif request.args and try_url_params: request_data = mixed(request.args) elif (request.data and request.data != u'' and request.content_type != u'multipart/form-data'): try: request_data = request.get_json() except BadRequest, e: raise ValueError(u'Error decoding JSON data. ' 'Error: %r ' 'JSON data extracted from the request: %r' % (e, request_data)) if not isinstance(request_data, dict): raise ValueError(u'Request data JSON decoded to %r but ' 'it needs to be a dictionary.' % request_data) if request.method == u'PUT' and not request_data: raise ValueError(u'Invalid request. Please use the POST method for ' 'your request') log.debug(u'Request data extracted: %r', request_data)
def _get_request_data(try_url_params=False): u'''Returns a dictionary, extracted from a request. If there is no data, None or "" is returned. ValueError will be raised if the data is not a JSON-formatted dict. The data is retrieved as a JSON-encoded dictionary from the request body. Or, if the `try_url_params` argument is True and the request is a GET request, then an attempt is made to read the data from the url parameters of the request. try_url_params If try_url_params is False, then the data_dict is read from the request body. If try_url_params is True and the request is a GET request then the data is read from the url parameters. The resulting dict will only be 1 level deep, with the url-param fields being the keys. If a single key has more than one value specified, then the value will be a list of strings, otherwise just a string. ''' def mixed(multi_dict): u'''Return a dict with values being lists if they have more than one item or a string otherwise ''' out = {} for key, value in multi_dict.to_dict(flat=False).iteritems(): out[key] = value[0] if len(value) == 1 else value return out if not try_url_params and request.method == u'GET': raise ValueError(u'Invalid request. Please use POST method ' 'for your request') request_data = {} if request.method in [u'POST', u'PUT'] and request.form: if (len(request.form.values()) == 1 and request.form.values()[0] in [u'1', u'']): try: request_data = json.loads(request.form.keys()[0]) except ValueError as e: raise ValueError( u'Error decoding JSON data. ' 'Error: %r ' 'JSON data extracted from the request: %r' % (e, request_data)) else: request_data = mixed(request.form) elif request.args and try_url_params: request_data = mixed(request.args) elif (request.data and request.data != u'' and request.content_type != u'multipart/form-data'): try: request_data = request.get_json() except BadRequest as e: raise ValueError(u'Error decoding JSON data. ' 'Error: %r ' 'JSON data extracted from the request: %r' % (e, request_data)) if not isinstance(request_data, dict): raise ValueError(u'Request data JSON decoded to %r but ' 'it needs to be a dictionary.' % request_data) if request.method == u'PUT' and not request_data: raise ValueError(u'Invalid request. Please use the POST method for ' 'your request') for field_name, file_ in request.files.iteritems(): request_data[field_name] = file_ log.debug(u'Request data extracted: %r', request_data) return request_data
try: request_data = json.loads(request.form.keys()[0]) except ValueError, e: raise ValueError( u'Error decoding JSON data. ' 'Error: %r ' 'JSON data extracted from the request: %r' % (e, request_data)) else: request_data = mixed(request.form) elif request.args and try_url_params: request_data = mixed(request.args) elif (request.data and request.data != u'' and request.content_type != u'multipart/form-data'): try: request_data = request.get_json() except BadRequest, e: raise ValueError(u'Error decoding JSON data. ' 'Error: %r ' 'JSON data extracted from the request: %r' % (e, request_data)) if not isinstance(request_data, dict): raise ValueError(u'Request data JSON decoded to %r but ' 'it needs to be a dictionary.' % request_data) if request.method == u'PUT' and not request_data: raise ValueError(u'Invalid request. Please use the POST method for ' 'your request') log.debug(u'Request data extracted: %r', request_data)