Upload files in Postman with FastAPI

Viewed 15

I have a promblem with Postman and FasrtAPI. Wrote thin code for FastAPI. First is for single image, second for mulriple. In FastAPI Swaager it all works fine, but my POST requests with files or images it fails, i got this

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

And here is the code

 FastAPI how to upload files
    @app.post("/upload")
    async def single(file: UploadFile = File(...)):
        with open(f'{file.filename}', "wb") as buffer:
            shutil.copyfileobj(file.file, buffer)
        return {'index.html': file.filename}
    #FastAPI how to upload files
    
    FastAPI how to upload multiple files
    @app.post("/img")
    async def upload_images(files: List[UploadFile] = File(...)):
        for file in files:
            with open(f'{file.filename}', "wb") as buffer:
                shutil.copyfileobj(file.file, buffer)
        
        return {"file_name": file.filename}
    FastAPI how to upload multiple files
0 Answers
Related