【问题标题】:fastapi post method call with and without trailing slashes带有和不带有斜杠的 fastapi post 方法调用
【发布时间】:2021-12-28 14:51:16
【问题描述】:

我正在尝试将 fastapi post 方法编写为:

@app.post('/method/final_path/', tags=['method/final_path'])

当我打电话给邮递员时 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'])

与邮递员通话:

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

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

版本:
fastapi-0.63
starlette-0.13.6

提前致谢。

【问题讨论】:

  • 您是否尝试过不使用斜杠而不是斜杠来定义它?
  • 是的,那么我也遇到了同样的问题,https://..../final_path 给出 405 错误
  • 对不起,我的错。没意识到,请忽略最后一条评论:当调用变为不带斜线时,https://..../final_path/ 给出 405 错误

标签: python-3.x fastapi starlette


【解决方案1】:

尝试使用APIRouter 来定义路由,而不是直接使用appAPIRouter 默认将redirect_slashes 设置为True

【讨论】:

    猜你喜欢
    • 2017-07-28
    • 2018-11-23
    • 2019-07-18
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 2013-08-24
    • 1970-01-01
    相关资源
    最近更新 更多