[Python] Send files in HTTP POST request with their json data [duplicate]

Discussão em 'Python' iniciado por Stack, Fevereiro 4, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to send HTTP request to my Python FASTAPI service with request details below.

    curl -X 'POST' \
    'http://localhost:8081/file/upload' \
    -H 'accept: application/json' \
    -H 'Content-Type: multipart/form-data' \
    -F 'files=@87869be4-07af-4089-b695-108593d6c027 (1).pdf;type=application/pdf' \
    -F 'files=@Holiday Schedule IN - 2025 1 (1).pdf;type=application/pdf' \
    -F 'files=@uploads_1234.pdf;type=application/pdf' \
    -F 'metadatas=,,{1, 2, 3, 4}'


    Below is request screenshot from swagger page. enter image description here

    In this request problem is if I don't want to send any metadata for first two files, then I have to include them even with null values. In curl command you can see that in metadatas their are two null values before {1, 2, 3, 4}. files are going differently instead of a list.

    Below is my python router code .

    @router.post("/upload")
    async def upload_files(
    files: List[UploadFile] = File(...),
    metadata_list: List[Optional[str]] = Form(None)
    ) -> dict:


    But I want List of a object in my router so that each object contains a file and its optional metadata. And from Swagger page, also I want like if someone click on add item, then two fields come first to choose file and second will contain its meta data which will be null if not specified. Not two independent lists for files and metadata as in my screenshot. Please provide code and explanation also, how it can be achieved.

    Please provide code and its little explanation if possible.

    Continue reading...

Compartilhe esta Página