1

I'm trying to upload a file from SwaggerUI and receive it at Flask back end request.files is empty. I don't care about the original file name.

My Swagger configuration is :

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

My app.py file look like that :

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

And the implementation of POST method is the following:

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)

All the prints are returning empty lists. ==> Any idea of what's going on?

2 Answers2

0

I think, that Flask returning an empty answer in a case, when in your uploading form was not set a name.

I don't know how to change code in your case, I know, that in pure html you have to change (it is wrong code) <input type="file"> to <input type="file" name="upfile"> - it is right code.

Viktor Ilienko
  • 817
  • 8
  • 15
0

Have you try

request.data

I see an answer here