[BUG] auto-rewrite doesn't take openapi_prefix into account
See original GitHub issueDescribe the bug
The problem is basically the one described here: https://github.com/tiangolo/fastapi/issues/51#issuecomment-522035784 My fastapi app is behind a reverse proxy that forwards all requests for /api/v1/* and rewrites them to /*. The application itself doesn’t know that but the FastAPI object which has openapi_prefix=“/api/v1”.
If I have an endpoint
@app.get("/hello")
def foo():
return "hello"
and I query /api/v1/hello, it works fine. If I add an extra slash at the end, a redirect occurs but tries to send the user to /hello instead of /api/v1/hello. I’m not sure if it’s a bug or if I don’t use the app correctly. Maybe it would be simpler for me to make the fastapi app aware of the /api/v1 prefix and remove the rewrite rule? What do you think?
To Reproduce
Behind a reverse proxy forwarding the requests for /api/v1/* and rewriting those to /*
- Create a file with:
from fastapi import FastAPI
app = FastAPI(openapi_prefix="/api/v1")
@app.get("/hello")
def read_root():
return {"Hello": "World"}
- Open the browser and call the endpoint
/api/v1/hello/
. - It returns a 404 because there is a redirect to /hello.
- But I expected the redirect to be to /api/v1/hello.
Expected behavior
The redirection shouldn’t remove the openapi_prefix.
Environment
- OS: Linux / Windows
- FastAPI Version 0.52.0 from pip
- python 3.7.4.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top GitHub Comments
@tiangolo Hello. I just tested my app by replacing the openapi_prefix parameter by root_path on the FastAPI object: it works like a charm. I’ll close this issue. Thanks a lot!
@tiangolo will start investigating this today