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


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


You can examine the url through several Request fields:

Imagine your application is listening on the following application root:

And a user requests the following URI:

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

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

An example of using this:

from flask import request

def index():
    return request.base_url

Answered By – icecrime

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published