Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

260
Views
Uploading images in FastAPI post request causes 400 Bad Request

I would like to get image via post request and then read it. I'm trying to do is this way:

import numpy as np
from PIL import Image
from fastapi import FastAPI, File, UploadFile, HTTPException, Depends
app = FastAPI()
@app.post("/predict_image")
    @logger.catch
    def predict_image(predict_image: UploadFile = File(...)):
        logger.info('predict_image POST request performed')
        try:
            pil_image = np.array(Image.open(predict_image.file))
        except:
            raise HTTPException(
                status_code=HTTP_422_UNPROCESSABLE_ENTITY, detail="Unable to process file"
            )
        pred = pil_image.shape
        logger.info('predict_image POST request performed, shape {}'.format(pred))
    return {'input_shape': pred}

Calling post request returns INFO: 127.0.0.1:59364 - "POST /predict_image HTTP/1.1" 400 Bad Request

enter image description here

How to fix it?

UPD:

Example from official tutorial return same exception:

@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
    return {"filename": file.filename}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You probably need to install python-multipart.

Just:

pip install python-multipart
over 4 years ago · Santiago Trujillo Report

0

Fixed this way:

@app.post("/predict_image/")
@logger.catch
def make_inference(file: bytes = File(...)):
    try:
        pil_image = np.array(Image.open(BytesIO(file)))

    except:
        raise HTTPException(
            status_code=HTTP_422_UNPROCESSABLE_ENTITY, detail="Unable to process file"
        )
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!