带或不带尾部斜杠的 fastapi post 方法调用

ani*_*jha 7 python-3.x starlette fastapi

我试图编写一个 fastapi post 方法:

@app.post('/method/final_path/', tags=['method/final_path'])
Run Code Online (Sandbox Code Playgroud)

当我以https://......./method/final_path/进行邮递员调用时,
我得到了预期的结果,但如果调用更改为 https://......./method/ Final_path
我收到 405-method not allowed。

根据 FastAPI 文档,尾部斜杠应该无关紧要,所以理想情况下

@app.post('/method/final_path/', tags=['method/final_path'])
@app.post('/method/final_path', tags=['method/final_path'])
Run Code Online (Sandbox Code Playgroud)

与邮递员电话:

  • https://......./method/final_path/
  • https://......./method/final_path

所有上述 4 种组合应该给出相同的结果。那我做错了什么?

版本:
fastapi-0.63
starlette-0.13.6

提前致谢。

Raf*_*san 6

尝试使用APIRouter来定义路由而不是直接使用app. 默认情况下已设置APIRouter为。redirect_slashesTrue