Jansindl3r
Jansindl3r

Reputation: 399

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

Upvotes: 4

Views: 4650

Answers (1)

Anlis
Anlis

Reputation: 839

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.

Upvotes: 10

Related Questions