422 Unprocessable Entity - value is not a valid dict when changing name of a file

Viewed 23

im trying to build an API route that handle a video and a name of file, so the api store the video in a path of my computer while being renamed with the name that is sended via the API.

When i put a class having a name variable in argument of the function i keep getting "422 Unprocessable Entity - value is not a valid dict when changing name of a file".

    @app.post("/uploadfile")
async def upload_file(name_of_file: _schemas.UploadVideo, file: UploadFile = File(...)):
    file_name = name_of_file

    file_location = f"/Users/User/videos/{file_name}"
    with open(file_location, "wb+") as file_object:
        file_object.write(file.file.read())
    return {f"file{file_name} saved at {file_location}"}

But when i put a simple str in argument it works.

@app.post("/uploadfile")
async def upload_file(name_of_file: str, file: UploadFile = File(...)):
    file_name = name_of_file

    file_location = f"/Users/User/videos/{file_name}"
    with open(file_location, "wb+") as file_object:
        file_object.write(file.file.read())
    return {f"file{file_name} saved at {file_location}"}

But i would like to know why it doesnt work with a class ? Thanks ! There is the class and the whole error.

class UploadVideo(pydantic.BaseModel):
    name: str

The error.

    {
  "detail": [
    {
      "loc": [
        "body",
        "name_of_file"
      ],
      "msg": "value is not a valid dict",
      "type": "type_error.dict"
    }
  ]
}
0 Answers
Related