I want to post a file and a json object to a route in my FastAPI in only one request, but doing so results in errors like:
422
{"detail":[{"loc":["body"],"msg":"value is not a valid list","type":"type_error.list"},{"loc":["body"],"msg":"value is not a valid dict","type":"type_error.dict"}]}
This is the client file:
import requests
url = "http://localhost:8000/"
file = "..."
data = {...}
files = {"pdf": (file, open(file, "rb"), "application/pdf")}
response = requests.post(url, json=data, files=files)
I have also tried setting the headers to multipart/form-data
and multipart/reated
This is what my server file looks like:
...
class SomeMetadata(BaseModel): ...
...
@app.post("/")
def submit(someMetadata: SomeMetadata = Body(...), pdf: UploadFile = File(...)):
return ...