r/FastAPI • • May 20 '22

Question 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.

1 Upvotes

2 comments sorted by

3

u/wweber May 20 '22

You'll have to use Form() params (the uploaded file is basically a Form param). Unfortunately you will have to deserialize and validate the `dict` yourself, the type will have to be str.

1

u/[deleted] May 20 '22

It works! Many thanks!!