How to send file to fastapi endpoint using postman

后端 未结 2 1833
深忆病人
深忆病人 2020-12-12 05:02

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 understan

相关标签:
2条回答
  • 2020-12-12 05:06

    As mentioned in the response, I could see the key for the file uploaded is missing. Mention the key for the file in body params as file.

    0 讨论(0)
  • 2020-12-12 05:25

    My code:

    from fastapi import FastAPI, UploadFile, File
    
    
    app = FastAPI()
    
    
    @app.post("/file/")
    async def create_upload_file(file: UploadFile = File(...)):
        return {"filename": file.filename}
    

    Setup in Postman

    As stated in https://github.com/tiangolo/fastapi/issues/1653, the parameter name for the file is the key value that you have to use. Before you were using key=file and value=image.png (or whatever). Instead, FastAPI accepts file=image.png. Thus the error, since the file is necessary, but it is not present (at least, the key with that name is not present).

    I tested it with Postman v7.16.1

    Let me know if you still have problems.

    0 讨论(0)
提交回复
热议问题