How to get current path in fastapi with domain?

by scotty_walker , in category: Third Party Scripts , 11 days ago

How to get current path in fastapi with domain?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by edmond_brakus , 10 days ago

@scotty_walker 

In FastAPI, you can get the current path along with the domain by using the request parameter provided by the framework. You can access the current path and domain using the url_for function from the fastapi.routing module.


Here is an example of how to get the current path with domain in FastAPI:

1
2
3
4
5
6
7
8
9
from fastapi import FastAPI, Request
from fastapi.routing import request_url

app = FastAPI()

@app.get("/")
async def get_current_path(request: Request):
    current_path = request_url(request).url
    return {"current_path": current_path}


When you make a request to the endpoint / of your FastAPI application, you will receive a JSON response containing the current path with the domain.