flask reqparse how to get empty list using action='append' (not by default)

Viewed 200

i need to get empty list if it in request but also need to get None if argument don't given at all in request

if i defined argument like that

parser.add_argument('participating',type=int, nullable=False, action='append')

then if i do request like this

print(post('http://localhost:80/api/', json={'participating':[]}).json())

parser sees it like 'participating':None

and if i do that request

print(post('http://localhost:80/api/', json={}).json())

then parser also sees it like 'participating':None

1 Answers

i solved it by check for exist key in flask.request.json

if not args['participating'] and 'participating' in flask.request.json and not flask.request.json['participating']:
    args['participating'] = flask.request.json['participating']

in that case values was:
args['participating'] = None
flask.request.json['participating'] = []
(but i open for any others solutions)

Related