'How to pass both file (uploaded) and dict params to POST request in FAST API?
Specifically, I want the example below to work:
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}
Currently, while trying to create a POST request passing any valid dict to grid parameter and upload some file, FAST API raises the error:
"detail": [
{
"loc": [
"body",
"grid"
],
"msg": "value is not a valid dict",
"type": "type_error.dict"
}
]
}
At the same time, if I use only one of these parameters inside the function, it works well.
I am calling (testing) my API this way:
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())
as well as via in-built Swagger UI.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

