小编Ali*_*nte的帖子

使用 FastApi 测试上传多个文件

我正在尝试测试将文件上传到 FastApi,但我不断收到 422 验证错误。文件上传在OpenApi接口下可以正常上传,但在下面的测试中不行。

路由器:

@router.post("/files")
def file_contents(files: List[UploadFile]):
    return someprocessing(files)
Run Code Online (Sandbox Code Playgroud)

测试(使用TestClientFastApi):

response = client.post(
    url="/files",
    files={"files": ("file.xlsx", open("./test_files/file.xlsx", "rb"))},
    headers={**auth_headers, **{"Content-Type": "multipart/form-data"}},
)
Run Code Online (Sandbox Code Playgroud)

错误:

{"detail":[{"loc":["body"],"msg":"value is not a valid dict","type":"type_error.dict"}]}
Run Code Online (Sandbox Code Playgroud)

更新:很好,我正在将文件发送到不同的网址...

pytest python-3.x fastapi

1
推荐指数
1
解决办法
4980
查看次数

标签 统计

fastapi ×1

pytest ×1

python-3.x ×1