[Python] FastAPI throws 422 error when trying to add a File Upload [duplicate]

Discussão em 'Python' iniciado por Stack, Novembro 28, 2024.

  1. Stack

    Stack Membro Participativo

    I want to have a call that takes in both a json form for a 'product' and a image file to save along with it. I've tried doing so with this code

    @app.post("/products")
    def create_product(product: Product, file:UploadFile = File(...), db: Session = Depends(get_db)):

    product_model = models.Products()
    product_model.name = product.product_name
    product_model.description = product.description
    product_model.remaining_quantity = product.start_quantity
    product_model.start_quantity = product.start_quantity
    product_model.limit_per_user = product.limit

    product_model.is_featured = product.is_featured
    product_model.is_open = product.is_open



    db.add(product_model)
    db.commit()

    return product


    Before I added the functionality of saving the image, I realized the call wouldn't work anymore and I keep getting a

    {
    "detail": [
    {
    "loc": [
    "body",
    "product"
    ],
    "msg": "value is not a valid dict",
    "type": "type_error.dict"
    }
    ]
    }


    I don't know why this is the case.

    I've tried separating the image upload to another class, and that actually worked, but I want them to be all in one call. Is there something wrong with how I'm writing my method header?

    Continue reading...

Compartilhe esta Página