2

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?

DuckQueen
  • 772
  • 10
  • 62
  • 134

1 Answers1

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.

Eric
  • 356
  • 3
  • 9
Josh
  • 1,556
  • 1
  • 10
  • 21