pythonflaskquery-string

Get raw query string in flask


Is there a way to get the raw query string or a list of query string parameters in Flask? I know how to get query string parameters with request.args.get('key'), but I would like to be able to take in variable query strings and process them myself. Is this possible?


Solution

  • There are several request attributes that let you access the raw url:

    Imagine your application is listening on the following URL:

    http://www.example.com/myapplication
    

    And a user requests the following URL:

    http://www.example.com/myapplication/page.html?x=y
    

    In this case the values of the above mentioned attributes would be the following:

    path          /page.html
    script_root   /myapplication
    base_url      http://www.example.com/myapplication/page.html
    url           http://www.example.com/myapplication/page.html?x=y
    url_root      http://www.example.com/myapplication/
    

    These, eventually with the help of urlparse (urllib.parse in python 3), will let you extract the information you need:

     >>> from urlparse import urlparse
     >>> urlparse(request.url).query
     'x=y'