How do you get a query string on Flask?

Posted on

Question :

How do you get a query string on Flask?

Not obvious from the flask documention on how to get the query string. I am new, looked at the docs, could not find!


def data():
    return render_template("data.html")
Asked By: Tampa


Answer #1:

from flask import request

def data():
    # here we want to get the value of user (i.e. ?user=some-value)
    user = request.args.get('user')
Answered By: Kracekumar

Answer #2:

The full URL is available as request.url, and the query string is available as request.query_string.decode().

Here’s an example:

from flask import request

def adhoc_test():

    return request.query_string

To access an individual known param passed in the query string, you can use request.args.get('param'). This is the “right” way to do it, as far as I know.

ETA: Before you go further, you should ask yourself why you want the query string. I’ve never had to pull in the raw string – Flask has mechanisms for accessing it in an abstracted way. You should use those unless you have a compelling reason not to.

Answered By: Lyndsy Simon

Answer #3:

I came here looking for the query string, not how to get values from the query string.

request.query_string returns the URL parameters as raw byte string (Ref 1).

Example of using request.query_string:

from flask import Flask, request

app = Flask(__name__)

@app.route('/data', methods=['GET'])
def get_query_string():
    return request.query_string

if __name__ == '__main__':


query parameters in Flask route


  1. Official API documentation on query_string
Answered By: Michael Ribbons

Answer #4:

Werkzeug/Flask as already parsed everything for you. No need to do the same work again with urlparse:

from flask import request

def data():
    query_string = request.query_string  ## There is it
    return render_template("data.html")

The full documentation for the request and response objects is in Werkzeug:

Answered By: Simon Sapin

Answer #5:

We can do this by using request.query_string.


Lets consider

from my_script import get_url_params

@app.route('/web_url/', methods=('get', 'post'))
def get_url_params_index():
    return Response(get_url_params())

You also make it more modular by using Flask Blueprints –

Lets consider first name is being passed as a part of query string

## here is

## import required flask packages
from flask import request
def get_url_params():
    ## you might further need to format the URL params through escape.    
    firstName = request.args.get('first_name') 
    return firstName

As you see this is just a small example – you can fetch multiple values + formate those and use it or pass it onto the template file.

Answer #6:

Try like this for query string:

from flask import Flask, request

app = Flask(__name__)

@app.route('/parameters', methods=['GET'])
def query_strings():

    args1 = request.args['args1']
    args2 = request.args['args2']
    args3 = request.args['args3']

    return '''<h1>The Query String are...{}:{}:{}</h1>''' .format(args1,args2,args3)

if __name__ == '__main__':

enter image description here

Answered By: Sankar guru

Answer #7:

Every form of the query string retrievable from flask request object as described in O’Reilly Flask Web Devleopment:

From O’Reilly Flask Web Development, and as stated by Manan Gouhari earlier, first you need to import request:

from flask import request

request is an object exposed by Flask as a context variable named (you guessed it) request. As its name suggests, it contains all the information that the client included in the HTTP request. This object has many attributes and methods that you can retrieve and call, respectively.

You have quite a few request attributes which contain the query string from which to choose. Here I will list every attribute that contains in any way the query string, as well as a description from the O’Reilly book of that attribute.

First there is args which is “a dictionary with all the arguments passed in the query string of the URL.” So if you want the query string parsed into a dictionary, you’d do something like this:

from flask import request

    queryStringDict = request.args

(As others have pointed out, you can also use .get('<arg_name>') to get a specific value from the dictionary)

Then, there is the form attribute, which does not contain the query string, but which is included in part of another attribute that does include the query string which I will list momentarily. First, though, form is “A dictionary with all the form fields submitted with the request.” I say that to say this: there is another dictionary attribute available in the flask request object called values. values is “A dictionary that combines the values in form and args.” Retrieving that would look something like this:

from flask import request

    formFieldsAndQueryStringDict = request.values

(Again, use .get('<arg_name>') to get a specific item out of the dictionary)

Another option is query_string which is “The query string portion of the URL, as a raw binary value.” Example of that:

from flask import request

    queryStringRaw = request.query_string

Then as an added bonus there is full_path which is “The path and query string portions of the URL.” Por ejemplo:

from flask import request

    pathWithQueryString = request.full_path

And finally, url, “The complete URL requested by the client” (which includes the query string):

from flask import request

    pathWithQueryString = request.url

Happy hacking 🙂

Answered By: 23r0c001

Answer #8:

If the request if GET and we passed some query parameters then,

fro`enter code here`m flask import request
def data():
   if request.method == 'GET':
      # Get the parameters by key
      arg1 = request.args.get('arg1')
      arg2 = request.args.get('arg2')
      # Generate the query string
      query_string="?arg1={0}&arg2={1}".format(arg1, arg2)
      return render_template("data.html", query_string=query_string)

Leave a Reply

Your email address will not be published.