How do you access the query string in Flask routes?
Query strings are usually made of query parameters. Flask provides an out-of-the-box solution to access parsed query parameters. In most cases, accessing query parameters is enough. But if you want to access the query string itself, you can do so by accessing the
query_string property of the request object.
Adding a query string to the URL is a common way to pass data to a web app. The query string is made up of arbitrary keys and values that are added to the URL after a question mark (
For example, in the following URL:
The query string is
language=python. Inside our app, we refer to ‘language’ as a query parameter and ‘python’ as its value.
Query parameters can be separated by an ampersand (
&) character, like so:
Flask provides a request object that contains all sorts of information about the current HTTP request.
For example, you can access the complete URL with the query string by accessing the
url property. Like so:
# import main Flask class and request object from flask import Flask, request # create the Flask app app = Flask(__name__) @app.route('/') def query_example(): print(request.url) # request.url property return 'Query String Example' if __name__ == '__main__': app.run(debug=True, port=5000)
If you run the above code, and send a request to
You will get the following output on your terminal:
If you want just the query string, you can access it like so:
You will get the following output:
This is the raw value of the query string. The preferred way to access the query parameters is to use the