在Swagger中上传文件并在Flask后端接收 [英] Upload a file in Swagger and receive at Flask backend

查看:75
本文介绍了在Swagger中上传文件并在Flask后端接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Swagger和Flask上传文件.我具有以下配置.

I'm trying to upload a file using Swagger and Flask. I've the following configuration for 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 ),它什么也不会返回.

I do get the option for uploading a file, but when I try to receive it at the backend,(using print request.files) it returns me nothing.

我如何在后端接收文件(在摇摇欲坠级别选择).??

How can I receive the file (selected at swagger level) at the backend.??

推荐答案

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

iFile = request.files.getlist('file')[0]- This command reads the file uploaded via swagger UI.

这篇关于在Swagger中上传文件并在Flask后端接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆