You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've got a problem while sending JSON data with a list in it using GET request.
I have a JSON like
{
"dataset": [0.5, 0.5]
}
And when I am trying to parse it from the request using reqparse.RequestParser I am getting an error
'float' object is not iterable
It works fine when arg type is str or int or float, but for the list, there is trouble with this.
Some code below.
I've made a helper method to setup a request parameters parsing
defparse_arguments(arguments):
""" Method to parse request arguments :param arguments: list of arguments to parse :return: list of parsed arguments """# create arguments parser and add argumentsparser=reqparse.RequestParser(bundle_errors=True)
forarginarguments:
parser.add_argument(arg.get('name', None), type=arg.get('type', None),
help=arg.get('help', ""), location=ARGS_LOCATIONS_LIST,
required=arg.get('required', False), action=arg.get('action', None))
# hack to get json from get requesttry:
_=request.get_json(force=True)
exceptHTTPException:
print("No json found in this request. Trying to parse args from values")
# parsing argument for requestreturnparser.parse_args()
And parameters for the request setup looks like this
# list of arguments for the requestarguments= [
{"name": "dataset", "type": list, "help": "dataset must be list", "required": True, "action": "append"}
]
# parsing argumentsargs=parse_arguments(arguments)
And with the code above, I get an error.
I've made a workaround to fix this problem for me, but I think it should not work like this.
defparse_arguments(arguments):
""" Method to parse request arguments :param arguments: list of arguments to parse :return: list of parsed arguments """# create arguments parser and add argumentsparser=reqparse.RequestParser(bundle_errors=True)
forarginarguments:
parser.add_argument(arg.get('name', None), type=arg.get('type', None),
help=arg.get('help', ""), location=ARGS_LOCATIONS_LIST,
required=arg.get('required', False), action=arg.get('action', None))
# hack to get json from get requesttry:
json_values=request.get_json(force=True) # getting values from request json directlyparser_values=parser.parse_args() # parsing values from request using reqparseforkey, valueinparser_values.items():
# if we have type error while parsing arguments - replace it from jsonifisinstance(parser_values[key], TypeError):
parser_values[key] =json_values.get(key, None)
returnparser_valuesexceptHTTPException:
print("No json found in this request. Trying to parse args from values")
# parsing argument for requestreturnparser.parse_args()
So the question is: am I doing something wrong or this is a bug?
The text was updated successfully, but these errors were encountered:
I've got a problem while sending JSON data with a list in it using GET request.
I have a JSON like
And when I am trying to parse it from the request using reqparse.RequestParser I am getting an error
It works fine when arg type is str or int or float, but for the list, there is trouble with this.
Some code below.
I've made a helper method to setup a request parameters parsing
And parameters for the request setup looks like this
And with the code above, I get an error.
I've made a workaround to fix this problem for me, but I think it should not work like this.
So the question is: am I doing something wrong or this is a bug?
The text was updated successfully, but these errors were encountered: