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

178,952

Solution 1

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.

Solution 2

another example:

request:

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

then:

request.method:              GET
request.url:                 http://127.0.0.1:5000/alert/dingding/test?x=y
request.base_url:            http://127.0.0.1:5000/alert/dingding/test
request.url_charset:         utf-8
request.url_root:            http://127.0.0.1:5000/
str(request.url_rule):       /alert/dingding/test
request.host_url:            http://127.0.0.1:5000/
request.host:                127.0.0.1:5000
request.script_root:
request.path:                /alert/dingding/test
request.full_path:           /alert/dingding/test?x=y

request.args:                ImmutableMultiDict([('x', 'y')])
request.args.get('x'):       y

Solution 3

you should try:

request.url 

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

Solution 4

If you are using Python, I would suggest by exploring the request object:

dir(request)

Since the object support the method dict:

request.__dict__

It can be printed or saved. I use it to log 404 codes in Flask:

@app.errorhandler(404)
def not_found(e):
    with open("./404.csv", "a") as f:
        f.write(f'{datetime.datetime.now()},{request.__dict__}\n')
    return send_file('static/images/Darknet-404-Page-Concept.png', mimetype='image/png')
Share:
178,952
Dogukan Tufekci
Author by

Dogukan Tufekci

Updated on July 08, 2022

Comments

  • Dogukan Tufekci
    Dogukan Tufekci almost 2 years

    I want to detect if the request came from the localhost:5000 or foo.herokuapp.com host and what path was requested. How do I get this information about a Flask request?

  • Vadim
    Vadim about 8 years
    I am trying to get Request.root_url and as return I get only <werkzeug.utils.cached_property object> instead of nicely formatted http://www.example.com/myapplication/. Or this feature does not work on localhost?
  • selfboot
    selfboot almost 8 years
    @Vadim You should use request.root_url, not Request.root_url.
  • Ulysse BN
    Ulysse BN over 7 years
    new to Flask, i didn’t know where request object come from and how it works, here it is: flask.pocoo.org/docs/0.12/reqcontext
  • zerocog
    zerocog almost 7 years
    request.url_root works for me, whereas request.root_url and Request.root_url fail. Therefore, watch for the cap 'R' and url_root versus root_url
  • moto
    moto over 5 years
    url_root returns http://www.example.com/ not http://www.example.com/myapplication/ base_url returns http://www.example.com/myapplication/
  • pfabri
    pfabri almost 5 years
    This should be the accepted answer, as it has a lot more detail to it.
  • Tao Starbow
    Tao Starbow almost 5 years
    This is a great answer. Very useful and complete.
  • user4772933
    user4772933 over 4 years
    my app route is like @app.route('/index/<int:id1>/<int:id2>') how can i guest path from request without variables. I expect /index in result. Which flask function to use?
  • Peilonrayz
    Peilonrayz over 3 years
    @Ian Request is a class where you interact with them through request which is an instance. This answer doesn't talk about the instance only the class. Leaving one to wonder "how I getz instance?"
  • booshong
    booshong over 3 years
    @Peilonrayz that's a good point! The answer could be more explicit with how to use/reference those fields, though arguably the linked documentation helps with that. (and to be super pedantic, request is actually a proxy). But you're right; For the average user an explicit reference to plain request.path etc would be easier to digest.
  • AnnieFromTaiwan
    AnnieFromTaiwan over 3 years
    For people who want to use request.full_path, suggest to use request.environ['RAW_URI'] instead. Because, when the actual full query path is /alert/dingding/test, request.full_path returns /alert/dingding/test?, an extra question mark will be added to the result, which might not be desirable.
  • Abhay Bh
    Abhay Bh over 2 years
    Thanks! This detailed information helped a lot. Really appreciated.
  • Anonymous12332313
    Anonymous12332313 over 2 years
    This is a great and very detailed answer
  • PYK
    PYK over 2 years
    request.remote_addr for 127.0.0.1