How do I get the different parts of a Flask request's url? How do I get the different parts of a Flask request's url? python python

How do I get the different parts of a Flask request's url?


You can examine the url through several Request fields:

Imagine your application is listening on the following application root:

http://www.example.com/myapplication

And a user requests the following URI:

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

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

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

You can easily extract the host part with the appropriate splits.


another example:

request:

curl -XGET http://127.0.0.1:5000/alert/dingding/test?x=y

then:

request.method:              GETrequest.url:                 http://127.0.0.1:5000/alert/dingding/test?x=yrequest.base_url:            http://127.0.0.1:5000/alert/dingding/testrequest.url_charset:         utf-8request.url_root:            http://127.0.0.1:5000/str(request.url_rule):       /alert/dingding/testrequest.host_url:            http://127.0.0.1:5000/request.host:                127.0.0.1:5000request.script_root:request.path:                /alert/dingding/testrequest.full_path:           /alert/dingding/test?x=yrequest.args:                ImmutableMultiDict([('x', 'y')])request.args.get('x'):       y


you should try:

request.url 

It suppose to work always, even on localhost (just did it).