【发布时间】:2021-06-23 16:11:36
【问题描述】:
在 FAST API 文档中,列出了以下示例作为在传输文件时如何在发布请求中传递附加参数的参考。
https://fastapi.tiangolo.com/tutorial/request-forms-and-files/
from fastapi import FastAPI, File, Form, UploadFile
app = FastAPI()
@app.post("/files/")
async def create_file(
file: bytes = File(...), token: str = Form(...)
):
当我发出如下发布请求时,我得到:
requests.post(URL, files={'file': hdf5_file, 'token': 'teststring'})
422 Unprocessable Entity
{'detail': [{'loc': ['body', 'model_str'],
'msg': 'str type expected',
'type': 'type_error.str'}]}
为什么无法将 token 识别为字符串?从文档中并不清楚将表单与文件混合的发布请求应该是什么样子。
【问题讨论】:
标签: python python-requests fastapi