Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using request args in Flask for a variable URL

Tags:

python

flask

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.

like image 518
Malakaman Avatar asked Nov 01 '16 21:11

Malakaman


2 Answers

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)
like image 186
furas Avatar answered Oct 02 '22 15:10

furas


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.

like image 42
Jérôme Avatar answered Oct 02 '22 16:10

Jérôme