Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question marks in Flask Urls for routing [duplicate]

So, I have the following route in Flask:

@app.route("/menu-card/<google_place_id>", methods=['GET']) 

On navigating to http://127.0.0.1:5000/menu-card/ChIJAxXhIUMUrjsR5QOqVsQjCCI, I get the proper response.

However, then I tried changing the URL pattern as follows:

@app.route("/menu-card?id=<google_place_id>", methods=['GET'])

On navigating to http://127.0.0.1:5000/menu-card?id=ChIJAxXhIUMUrjsR5QOqVsQjCCI I now get a 404 error. What am I doing wrong ?

like image 474
Amistad Avatar asked Nov 17 '16 15:11

Amistad


1 Answers

The part after the ? is the query string, which you can get using request.args. So, your route should be:

@app.route("/menu-card", methods=['GET'])

and then you can get the id by using:

google_place_id = request.args.get('id', None)

where None is the default value, if id is not included in the url. You'll have to check if that it's not equal to None to make sure it has been passed.

Search the quickstart page for request.args to see another example.

like image 183
Christopher Shroba Avatar answered Sep 23 '22 09:09

Christopher Shroba