How to check for the existence of a get parameter in flask

Question:

I’m new to python and flask.

I know that I can fetch a GET parameter with request.args.get(varname);. I wanted to check whether a GET request to my server is specifying and optional parameter or not.

Flask documentation didn’t helped much.

Asked By: Thomas Abraham

||

Answers:

You can actually use the default value,

opt_param = request.args.get("something")
if opt_param is None:
    print "Argument not provided"
Answered By: Jakob Bowyer
page = request.args.get("page", 0, type=int)
Answered By: James Akwuh

A more Pythonic way to do the same would be using the in operator:

if 'varname' in request.args:
    # parameter 'varname' is specified
    varname = request.args.get('varname')
else:
    # parameter 'varname' is NOT specified
Answered By: kregus

You can check it with this code:

name = request.args.get("name", default=None, type=str)
Answered By: Cilas Amos
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.