在Swagger上传文件并在Flask后端接收

时间:2015-09-10 07:20:42

标签: python flask swagger

我尝试使用Swagger和Flask上传文件。我为swagger提供了以下配置。

"/user/register/": {
    "post": {
        "tags": ["user"],
        "summary": "Register a new user",
        "description": "",
        "operationId": "registerUser",
        "consumes": ["application/json"],
        "produces": ["application/json"],
        "parameters": [{
            "in": "body",
            "name": "body",
            "description": "User object that needs to be added.",
            "required": true,
            "schema": {
                "$ref": "#/definitions/User"
            }
        },
        {
            "name": "file",
            "in": "path",
            "description": "file to upload",
            "required": true,
            "type": "file"
        }]
    }
},

我确实获得了上传文件的选项,但是当我尝试在后端接收它时(使用print request.files)它没有返回任何内容。

如何在后端接收文件(在招摇级别选择)。??

1 个答案:

答案 0 :(得分:1)

iFile = request.files.getlist('file')[0] - 此命令读取通过swagger UI上传的文件。