Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flask optional argument in request.args

I am building a small API. For now I am having this workaround solution for optional arguments that need to be send to function later.

if 'A' in request.args:
    A = request.args['A']
else:
    A = 0

but I feel like that there must be something precise. This seems a bit unprofessional. Something like when I work argparse

parser.add_argument('--A', type=int, required=False')

Please, is it possible to shorten the first section a bit and use some of their functions for it? Thanks

like image 365
Jansindl3r Avatar asked Jan 02 '23 11:01

Jansindl3r


1 Answers

You can use get method something like:

a = request.args.get('A', None)

It puts 'A' value if it exists in args or just None if it doesn't. You can also replace None with any other data like 0 or 'nothing' or everything else.

like image 118
Andellys Avatar answered Jan 03 '23 23:01

Andellys