pythonhttphttprequestfastapihttp-referer

How to get the origin URL in FastAPI?


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?


Solution

  • 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.