Search code examples
python-2.7flaskrouteswerkzeug

Question marks in Flask Urls for routing


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 ?


Solution

  • 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.