首页 > 解决方案 > 发布 FastAPI docker-container 时出现 422 Unprocessable Entity 错误,但在未 dockerized 时有效

问题描述

使用相同的主体和配置,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 函数,所以我知道路由不是问题。

标签: dockerfastapipydantic

解决方案


缩进是一个有趣的概念。

{
  "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 的路由器解析它们时明显不同?

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


推荐阅读