FastAPI 出现 422 错误,但发送的数据是根据我使用的模式发送的

422 error on FastAPI but data sent was according to the schema that I used

这是来自 fastapi 交互式文档的 curl 请求

curl -X 'POST' \
  'http://127.0.0.1:8000/add/?api_key=stringstring' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F 'images_property={
  "text": "string",
  "characteristics": "string",
  "permissions": "public"
},{
  "text": "string",
  "characteristics": "string",
  "permissions": "public"
}' \
  -F 'images=@Screen Shot 2021-04-27 at 5.06.23 PM.png;type=image/png' \
  -F 'images=@Screen Shot 2021-04-27 at 5.06.23 PM.png;type=image/png'

这是我的函数头和模型

class ImagesProperty(BaseModel):

    text: str = Field(max_length=256)
    characteristics: str = Field(max_length=256)
    permissions: Optional[PermissionsEnum] = Field(
        PermissionsEnum.public.value)


@router.post("/add/")
async def add_images(api_key: str, images_property: List[ImagesProperty], images: List[UploadFile] = File(...)):

根据我的 curl 请求,我不知道为什么它总是一个字典,我认为它应该以任何数组开头,但它从来没有,我不知道为什么

我应该得到 200,但我的 ImagesProperty 模型得到了 422

我找到了原因,这是因为由于 HTTP 协议的限制,不允许将请求主体与文件或表单请求放在同一个 header 中。