How to get the URL of a request in a Flask application
Published on Aug. 22, 2023, 12:16 p.m.
To get the URL of a request in a Flask application, you can use the request
object provided by Flask. Here’s an example:
from flask import Flask, request
app = Flask(__name__)
@app.route('/')
def index():
url = request.url
return "The URL of the request is: {}".format(url)
In this example, we define a route function called index
that retrieves the URL of the request using request.url
. We then use the retrieved URL and return it as a string in the HTTP response.
Note that the url
attribute of the request
object contains the full URL of the request, including the scheme (http or https), the domain name, the path, and the query string. If you only need parts of the URL, you can access them using the base_url
, url_root
, and path
attributes of the request
object.