Is it possible to get the URL that a request came from in FastAPI?
For example, if I have an endpoint that is requested at api.mysite.com/endpoint
and a request is made to this endpoint from www.othersite.com
, is there a way that I can retrieve the string "www.othersite.com" in my endpoint function?
The premise of the question, which could be formulated as
a server can identify the URL from where a request came
is misguided. True, some HTTP requests (especially some of the requests issued by browsers) carry an Origin
header and/or a Referer
[sic] header . Also, the Forwarded
header , if present, contains information about where the request was issued. However, nothing in the HTTP specification requires that requests in general advertise where they came from.
Therefore, whether with FastAPI or some other server technology, there's no definite way of knowing where a request came from.
As per FastAPI documentation , and hence Starlette's :
Let's imagine you want to get the client's IP address/host inside of your path operation function.
@app.get("/items/{item_id}")
def read_root(item_id: str, request: Request):
client_host = request.client.host
return {"client_host": client_host, "item_id": item_id}
Please note, as pointed out by others, if you use nginx, you need to add headers to the nginx request to handle that data.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.