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

928
Visualizações
File upload using FastAPI returns error 422

I am using the example from the official documentation: https://fastapi.tiangolo.com/tutorial/request-files/#import-file

Server code:

@app.post("/uploadfile")
async def create_upload_file(data: UploadFile = File(...)):
    print("> uploaded file:",data.filename)
    return {"filename": data.filename}

Client code:

files = {'upload_file': open('config.txt', 'rb')}
resp = requests.post(
        url = URL,
        files = files)
print(resp.json())

The problem is that the server always responds with error 422:

{'detail': [{'loc': ['body', 'data'], 'msg': 'field required', 'type': 'value_error.missing'}]}

I am using Python 3 on both server and client and the python-multipart package is already installed.

Can someone please tell me what I am doing wrong, what am I missing, how should I fix the code?

Any hints are much appreciated.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Well, I just realized my error (which is not immediately obvious for newbies like me :))

The parameter you pass on client side ('upload_file')

files = {'upload_file': open('config.txt', 'rb')}

must match the parameter on the server side ('data'):

async def create_upload_file(data: UploadFile = File(...)):

So in order to work I had to rename on client side 'upload_file' to 'data':

files = {'data': open('config.txt', 'rb')} # renamed 'upload_file' to 'data'

That's it. Hopefully this helps some others as well.

over 4 years ago · Santiago Trujillo Relatório

0

On client side you should use the key you gave on server side. In your case, that is data. Thus, you should replace upload_file with data, as shown below:

files = {'data': open('config.txt', 'rb')}
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