【问题标题】:422 Unprocessable Entity error when POSTing a FastAPI docker-container but works when it's not dockerized发布 FastAPI docker-container 时出现 422 无法处理的实体错误,但在未 dockerized 时有效
【发布时间】:2021-06-22 19:34:33
【问题描述】:

使用相同的主体和配置,docker 容器显示 422 错误。 但是,如果我在我的 PC 上运行相同的 FastAPI 服务(没有 Docker),我可以成功获得我的结果。

Postman ping 容器时抛出以下错误:

{
    "detail": [
        {
            "loc": [
                "body"
            ],
            "msg": "value is not a valid dict",
            "type": "type_error.dict"
        }
    ]
}

供参考,这是冲突的函数:

 @router.post("/get_NERs")
 def get_NERs(self, artrel: ArticleRelevance):
     return artrel.dict()

ArticleRelevance 是:

class ArticleRelevance(BaseModel):
    title: str
    comments: List[str]

我能够从同一个 docker 容器中成功 ping GET 函数,所以我知道路由不是问题。

【问题讨论】:

  • 你是如何测试它的?您是否使用相同的 POST 内容/正文?
  • @Isabi,是的,内容/正文相同。
  • 您介意张贴吗?另外,如果可能的话,也为这两种情况发布邮递员配置

标签: docker fastapi pydantic


【解决方案1】:

缩进是一个有趣的概念。

{
  "headline": "Richest nations agree to end support for coal production overseas",
  "all_comments": ["Great, up next let’s shut down call centers in India", "Hope this hurts us here in Australia."]
}

{
      "headline": "Richest nations agree to end support for coal production overseas",
      "all_comments": ["Great, up next let’s shut down call centers in India", "Hope this hurts us here in Australia."]
}

FastAPI 的路由器解析它们时明显不同?

通过适当的缩进解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    • 2021-02-17
    相关资源
    最近更新 更多