5

My goal is to run a Flask app that will be given a URL of this form where the ids vary:

http://localhost:5000/Longword/game?firstid=123&secondid=456&thirdid=789

and return a simple page outputting something like

First id = 123, second id = 456, third id = 789

I run this script, and when hardcoding in an example URL I cannot get request args to return anything but None. I have tried formatting the int as a string and different things like that -- in no circumstances can I get request args to work.

import os
import json
from flask import Flask, request, render_template

app = Flask(__name__)

@app.route('/')
def main():
    return "on main home page"

@app.route('/longword/gameid=123&playerid=456')
def Longword():
    user = request.args.get('gameid')
    return "got hardcode %d" % gameid

My second issue, which will be tackled after I can get request args going, is that I cannot configure route() in such a way to handle variable URLs. I am only able to load pages by hardcoding them into route. I made a separate attempt to do this using sessions but was equally unsuccessful.

1
  • First use route('/longword/') without ?..... Then try http://localhost:5000/longword/?gameid=123&playerid=456. BTW: you get user = but later you use gameid which doesn't exists. Run code in debug mode to see more. Commented Nov 1, 2016 at 21:44

2 Answers 2

10

You need route('/longword/') without arguments after ?

Then you can run with arguments after ?

http://localhost:5000/longword/?gameid=123&playerid=456

In function you can get this arguments using request.args.get()

@app.route('/longword/')
def longword():

    gid = request.args.get('gameid')
    pid = request.args.get('playerid')

    return "GID: %s  PID: %s" % (gid, pid)

And last thing: arguments are strings even if you send numbers so you need %s instead of %d


BTW: you can run this url also without some argument ie.

http://localhost:5000/longword/?gameid=123
http://localhost:5000/longword/?playerid=456
http://localhost:5000/longword/

and you can set default value with request.args.get()

    gid = request.args.get('gameid', 'default gameid')
    pid = request.args.get('playerid', 'default playerid')

If you don't use own default value then request.args.get() will use None

    gid = request.args.get('gameid')
    pid = request.args.get('playerid')

    if not gid or not pid:
        return "You forgot gameid or playerid"

    return "GID: %s  PID: %s" % (gid, pid)
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, so my error was that I was not giving route everything before the "?" so I changed that second route line to _italic_@app.route('/longword/game/') and it worked. The comment below by @Jérôme does a nice job of giving more background. Thank you both!
9

Don't mix up query arguments and path parameters.

Path parameters

Path parameters are variables in the path part of the URL.

@app.route('/longword/game_id/')
def longword(game_id):
    [...]

You can optionally specify the type of such parameters:

@app.route('/longword/<int:game_id>/')
def longword(game_id):
    [...]

To call that function, you'd GET

http://localhost:5000/longword/123/

Those parameter can't be optional (unless you declare another route without them). The parameters are not named in the URL but the parameter / value association is not ambiguous. Think positional parameter.

Query arguments

Query arguments are in the query string (after the ?).

@app.route('/longword/')
def longword(game_id):
    game_id = request.args.get('gameid')
    return "got hardcode %d" % game_id

In this case, the parameters are unknown to the route. You can get them from the request object. Note there is no validation, here, so you must cover the cases where they are missing or of the wrong type.

To call that function, you'd GET

http://localhost:5000/longword/?gameid=123&playerid=456

The query arguments can be provided in any order, and they can be optional depending on what you do in the function. Think keyword parameter.

Note: To get validation for your function parameters, you may want to pay a look to webargs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.