-
DescriptionHow can I get current path? Additional contextIn nsidnev/fastapi-realworld-example-app, a Dependency function The path operation contains the dependency function |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Hi! from fastapi import FastAPI
from starlette.requests import Request
app = FastAPI()
@app.get("/path")
async def my_route(request: Request) -> None:
print(request.url.path) |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the discussion here, @nsidnev! 👏 🙇 And thanks @scil for coming back to close the issue 👍 |
Beta Was this translation helpful? Give feedback.
Hi!
Pathis a special function that helps to get the value from the request URL. Here it is used because this dependency is then applied to some other routes to get the article by slug. If you need to get the path from the URL after the host, then you can pass a request to theFastAPIroute (or dependency). After that, you can take path from URL fromstarlette.requests.Requestif this is what you need: