fastapi
fastapi copied to clipboard
Strip path doc trouble
I'm trying to work with a stripped path prefix (https://fastapi.tiangolo.com/advanced/behind-a-proxy/), it works totally fine for everything but the docs. Using the proxy and direct server works totally fine for the api routes, but the docs only work on the server itself and don't work on the proxy path.
I assumed this was related to the root_path. When I set that in FastAPI it seemed to do nothing for either the api or docs). When i set it via uvicorn it came through, but didn't effect anything (docs still didn't work)
It seems like it is looking for /openapi.json at the base-url without the path (hence why I assumed it need the root path). I assume I'm missing something obvious, so any direction would be greatly appreciated.
One more note, I can access /openapi.json at the end of path (/api/v1/openapi.json), but the docs themselves are looking for openapi at the base url (without the stripped path included aka just /)
Any suggestions would be appreciated.
I am having exactly the same issue, our apis are proxied like this: host/team-name/api-name. The docs are also look at /openapi.json at host/openapi.json where instead it is reachable at host/team-name/api-name/openapi.json.
I can't say this is the correct way of solving this issue but at least it works for me:
- set openapi_url to
./openapi.json
- configure servers to something like
[{"url": "http:proxy.host/api/v1", "description": "proxied"}]
You can set those as kwargs while initializing FastApi. For my use case I am setting them like this:
app.servers.extend([
{
"url": "http://localhost:8000",
"description": "Localhost"
},
{
"url": "http://proxy-host/team-name/api-name",
"description": "Dev cluster"
},
])
app.openapi_url = "./openapi.json"
the docs render without setting the servers but then 'try out' will not work because it will also use the stripped path
Just set the root_path for the added prefix by the proxy, this adds it to the docs html ref