So I want to get current request 'HTTP_REFERER'
. In Flask it is in request.environ.get('HTTP_REFERER', "")
. How to get one in fastapi?
Asked
Active
Viewed 1,357 times
2

DuckQueen
- 772
- 10
- 62
- 134
1 Answers
2
HTTP_REFERER
is just a request header, which you can access in a FastAPI endpoint using the referer
key as follows:
from fastapi import FastAPI, Request
app = FastAPI()
@app.get("/foo")
def foo(request: Request):
http_referer = request.headers.get('referer')
return {"http_referer": http_referer}
More information located in the FastAPI docs.
-
In FastAPI you don't use the HTTP_ prefix. Use: request.headers.get("Referer"). – Orlin Dec 27 '22 at 13:25