[Python] How do I send list of dictionaries as Body parameter together with Files in FastAPI?

Discussão em 'Python' iniciado por Stack, Julho 17, 2025.

  1. Stack

    Stack Membro Participativo

    In FastAPI, in order to pass a list of dictionaries, we usually define a pydantic schema and define a field as follows:

    param: List[schema_model]


    The issue I am facing is that I have files to attach to my request. I could not find a way to define a schema and File upload in router function. For that I am defining all the parameters (request body) as Body parameters like below:

    @router.post("/", response_model=DataModelOut)
    async def create_policy_details(request:Request,
    countryId: str = Body(...),
    policyDetails: List[dict] = Body(...),
    leaveTypeId: str = Body(...),
    branchIds: List[str] = Body(...),
    cityIds: List[str] = Body(...),
    files: List[UploadFile] = File(None)
    ):


    When I send a request using form-data option of postman, it is showing 0:value is not a valid dict for policyDetails parameter. I am sending [{"name":"name1","department":"d1"}]. It is saying not a valid dict, Even though I send valid dict. Can any one help me with this?

    DataModelOut class:

    class DataModelOut(BaseModel):
    message: str = ""
    id: str = ""
    input_data: dict = None
    result: List[dict] = []
    statusCode: int

    Continue reading...

Compartilhe esta Página