422 FastAPI post 请求中无法处理的实体

问题描述 投票:0回答:1

**当我尝试上传文件时出现此错误,我不知道该怎么办,

这是我的代码,**

from typing import Annotated

from fastapi import FastAPI, File, UploadFile

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

@app.post("/files/")
async def create_file(file: Annotated[bytes, File()]):
    return {"file_size": len(file)}



@app.post("/uploadyourfile/")
async def upload_your_file(file: UploadFile):
    return file.filename

在下面我向您提供来自邮递员的错误,并带有上述 422 错误代码,

{
    "detail": [
        {
            "type": "missing",
            "loc": [
                "body",
                "file"
            ],
            "msg": "Field required",
            "input": null,
            "url": "https://errors.pydantic.dev/2.6/v/missing"
        }
    ]
}

How i fix this issue

python http post client fastapi
1个回答
0
投票

错误响应指向缺少的“文件”字段。

确保在 Postman 中使用“form-data”正文类型,并使用键“file”上传文件(与处理程序中的参数名称相同)。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.