I faced the difficulty of testing api using postman. Through swagger file upload functionality works correctly, I get a saved file on my hard disk. I would like to understand how to do this with postman. I use the standard way to work with files which I use when working with django, flask.
Body -> form-data: key=file, value=image.jpeg
But with fastapi, I get an error
127.0.0.1:54294 - "POST /uploadfile/ HTTP/1.1" 422 Unprocessable Entity
main.py
@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
img = await file.read()
if file.content_type not in ['image/jpeg', 'image/png']:
raise HTTPException(status_code=406, detail="Please upload only .jpeg files")
async with aiofiles.open(f"{file.filename}", "wb") as f:
await f.write(img)
return {"filename": file.filename}
I also tried body -> binary: image.jpeg
. But got the same result
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…