0

I have a pydantic model:

class CreateModelCommand(BaseModel):
    name: Optional[str] = None
    description: Optional[str] = None

And I want to send it along two UploadFiles:

@app.post("/")
async def create_model(
        command: CreateModelCommand,
        binary: UploadFile,
        config: UploadFile,
):
    model = model_service.create_model(command, binary, config)

But since command is a pydantic model, FastApi tries to convert the entire body to a dict, as stated here: https://fastapi.tiangolo.com/tutorial/body-multiple-params/#multiple-body-parameters. This returns a Unprocessable Entity error, since it can't convert the files to dicts. Now I am asking if I can somehow disable this automatic detection or if I am simply using bad practise.

  • You can't mix a JSON body and form-data - you'll either have to use form-data for everything or a JSON body for everything - not mix them. They use different content-types when making a request, and when you include Pydantic model, you're asking for a JSON body to be submitted - when including the `UploadFile`, you're asking for a multipart/form-data body to be submitted, they're not compatible. – MatsLindh Dec 21 '22 at 13:44

0 Answers0