在使用FastAPI + uvicorn + Docker应用程序时,返回状态码200之前一直收到"307 Temporary Redirect"错误消息 - 如何返回状态码200?
在使用FastAPI + uvicorn + Docker应用程序时,返回状态码200之前一直收到"307 Temporary Redirect"错误消息 - 如何返回状态码200?
编辑:
我找到了问题,但不确定为什么会出现这种情况。每当我查询http://localhost:4001/hello/
并在末尾加上"/
"时,我会得到一个正确的200状态响应。我不明白为什么会这样。
原始帖子:
每当我向我的应用发送查询时,我都会得到一个307重定向。如何使我的应用返回常规的200状态,而不是通过307重定向。
以下是请求输出:
abm | INFO:172.18.0.1:46476 - "POST /hello HTTP/1.1" 307 Temporary Redirect abm | 返回苹果数据。没有什么特别的。 abm | INFO:172.18.0.1:46480 - "POST /hello/ HTTP/1.1" 200 OK
pytest返回:
E 断言 307 == 200 E + 其中 307 =.status_code test_main.py:24: AssertionError
在我的根目录/__init__.py
文件中:
from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware # from .configs import cors from .subapp import router_hello from .potato import router_potato from .apple import router_apple abm = FastAPI( title = "ABM" ) # potato.add_middleware(cors) abm.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) abm.include_router(router_hello.router) abm.include_router(router_potato.router) abm.include_router(router_apple.router) @abm.post("/test", status_code = 200) def test(): print('test') return 'test'
/subapp/router_hello.py
文件中:
router = APIRouter( prefix='/hello', tags=['hello'], ) @router.post("/", status_code = 200) def hello(req: helloBase, apple: appleHeader = Depends(set_apple_header), db: Session = Depends(get_db)) -> helloResponse: db_apple = apple_create(apple, db, req.name) if db_apple: return set_hello_res(db_apple.potato.api, db_apple.name, 1) else: return "null"
在/Dockerfile
中:
CMD ["uvicorn", "abm:abm", "--reload", "--proxy-headers", "--host", "0.0.0.0", "--port", "4001", "--forwarded-allow-ips", "*", "--log-level", "debug"]
我尝试了带有和不带有"--forwarded-allow-ips", "*"
部分。