Flask&Swagger:请求文件为空

时间:2018-08-27 18:08:14

标签: flask swagger swagger-ui

我正在尝试从SwaggerUI上传文件并在Flask后端接收它, request.files 为空。我不在乎原始文件名。

我的Swagger配置为:

parameters:
    - name: file
      in: formdata
      required: false
      type: file
    responses:
        '200':
            description: Firmware updated
            content:
                application/json:
                    schema:
                        $ref: '#/definitions/UpdateResponse'
    requestBody:
        content:
            multipart/form-data:
                schema:
                    type: string
                    format: binary
        description: log binary file

我的app.py文件如下所示:

app.add_url_rule(
    '/updates/update/',
    view_func=Update.as_view('updates_update'),
    methods=['POST']
)

if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0', port=5000

POST方法的实现如下:

class Update(MethodView):
    def post(self):
        """
        resets software and defaults
        ---
        tags:
            - system
        summary: resets software and defaults
        operationId: updateupdate
        description: resets defaults
        parameters:
        - name: file
          in: formdata
          required: false
          type: file
        responses:
            '200':
                description: Firmware updated
                content:
                    application/json:
                        schema:
                            $ref: '#/definitions/UpdateResponse'
        requestBody:
            content:
                multipart/form-data:
                    schema:
                        type: string
                        format: binary
            description: log binary file
        '400':
          description: 'update system not found'
        """
        data = {
            "id" : 0,
            "status" : "refused",
            "reason" : "battery_low"
        }
        print('Update Items')
        print('request.method', request.method)
        print('request.args', request.args)
        print('request.form', request.form)
        print('request.files', request.files)

所有照片都返回空列表。 ==>知道发生了什么吗?

2 个答案:

答案 0 :(得分:0)

我认为,如果在您的上传表单中未设置名称的情况下,Flask会返回一个空答案。

我不知道如何更改您的代码,我知道,在纯HTML中,您必须将{strong>错误的代码)从<input type="file">更改为{{1} }-这是正确的代码。

答案 1 :(得分:0)

您尝试过

  

request.data

我看到一个答案here