mad*_*ist 5 python request python-requests fastapi
具体来说,我希望下面的示例能够工作:
from fastapi import FastAPI
from fastapi import UploadFile
app = FastAPI()
@app.post("/check")
def foo(grid: dict, file: UploadFile):
return {"len_grid": len(grid), "filename": file.filename}
Run Code Online (Sandbox Code Playgroud)
目前,在尝试创建 POST 请求并将任何有效的字典传递给grid参数并上传一些文件时file,FAST API 会引发错误:
"detail": [
{
"loc": [
"body",
"grid"
],
"msg": "value is not a valid dict",
"type": "type_error.dict"
}
]
}
Run Code Online (Sandbox Code Playgroud)
同时,如果我在函数内仅使用这些参数之一,则效果很好。
我这样调用(测试)我的 API:
import requests
url = 'http://127.0.0.1:8000/check'
file = {'file': open('ny_pollution_events.csv', 'rb')}
payload = {"grid": {"name": "Alex", "age": 15}}
resp = requests.post(url=url, data=payload, files=file)
print(resp.json())
Run Code Online (Sandbox Code Playgroud)
以及通过内置的 Swagger UI。
| 归档时间: |
|
| 查看次数: |
2261 次 |
| 最近记录: |