Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

397
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda