DuckQueen
DuckQueen

Reputation: 802

What is fastApi analog for the flask request.environ.get('HTTP_REFERER', "")?

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?

Upvotes: 3

Views: 2500

Answers (1)

Josh
Josh

Reputation: 1976

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.

Upvotes: 3

Related Questions