Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

399
Vistas
How to send multiple files with Python requests to FastAPI server?

I have the following endpoint in my FastAPI server:

@app.post("/submit")
async def submit(file1: List[UploadFile] = File(...), file2: List[UploadFile] = File(...), file3: List[UploadFile] = File(...)):
    # do something
    return 'hello'

It works as expected if I use Swagger UI. However, I cannot figure out how to send a POST request to this endpoint using Python requests module. Here is what I've tried:

import requests

headers = {
    'accept': 'application/json',
    'Content-Type': 'multipart/form-data',
}

files = [
    ('file1', open('file1.zip', 'rb')), 
    ('file2', open('file2.txt', 'rb')),
    ('file3', open('file3.rar', 'rb')),
]

response = requests.post('http://localhost:8000/submit', headers=headers, files=files)

print(response.text)

But I am getting an error:

{"detail":"There was an error parsing the body"}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You should either define each file as a separate parameter in the endpoint, e.g., file1: UploadFile = File(...), file2: UploadFile = File(...), etc. (without using List), or, preferably, define a List of files using files: List[UploadFile] = File(...), as shown below (the below can be used to return the filenames of all uploaded files).

@app.post("/submit")
async def submitUPP(files: List[UploadFile] = File(...)):
    # do something
    return {"Uploaded Files:": [file.filename for file in files]}

On client side, you should send the list of files using the same key you gave in the endpoint (i.e., files in the case above) for each file.

files = [
    ('files', open('file1.zip', 'rb')), 
    ('files', open('file2.txt', 'rb')),
    ('files', open('file3.rar', 'rb')),
]

You may also want to have a look at this and this answer.

Important

In addition to the above, one shouldn't set the Content-Type in the headers to multipart/form-data when using files, but rather let Python requests set the Content-Type, as in addition to multipart/form-data, Content-Type must include the boundary string used to separate each body part in the multipart payload). Please have a look at the answers here and here. Setting the Content-Type in the way you do is the reason for the error you encountered, i.e., {'detail': 'There was an error parsing the body'}. Thus, you should avoid setting the Content-Type in the headers.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda