I have a simple route as below that written in FastAPI,
JavaScript
x
9
1
from fastapi import FastAPI
2
3
app = FastAPI()
4
5
6
@app.get("/foo/bar/{rand_int}/foo-bar/")
7
async def main(rand_int: int):
8
return {"path": f"https://some-domain.com/foo/bar/{rand_int}/foo-bar/?somethig=foo"}
9
How can I get the current path “programmatically” with,
- domain (
some-domain.com
) - path (
/foo/bar/{rand_int}/foo-bar/
) - and query parameters (
?somethig=foo
)
Advertisement
Answer
We can use the Request.url
-(starlette doc) API to get the various URL properties. To get the absolute URL, we need to use the Request.url._url
private API , as below
JavaScript
1
8
1
from fastapi import FastAPI, Request
2
3
app = FastAPI()
4
5
6
@app.get("/foo/bar/{rand_int}/foo-bar/")
7
async def main(rand_int: int, request: Request):
8
return {"raw_url": request.url._url}