2

I've got the following code for flask-restplus.

The POST request generates a Swagger UI that allows to upload a file.

Then I want the PUT request to also allow for a file to upload from Swagger UI and also provide a payload body.

For this code the Swagger UI is generated with an file browse and upload input field and a json body payload.

enter image description here

The problem is that the PUT request's api.payload returns None.

If I remove the @api.expect(upload_parser), the api.payload returns the fields defined by amazon_data_csv_update_model.

What am I doing wrong?

upload_parser = reqparse.RequestParser()
upload_parser.add_argument('file', location='files', type=FileStorage, required=True)


amazon_data_csv_update_model = api.model('AmazonDataCSVUpdateModel', {
    'uuid_to_update': fields.String(description='The UUID of the file to update', required=True)
})

@api.route('/amazon-data/upload')
@api.expect(upload_parser)
class AmazonDataCSVUpload(Resource):

    def post(self):
        upload_args = upload_parser.parse_args()
        uploaded_csv_file = upload_args['file']  # This is FileStorage instance
        json_uuid = AmazonHandler.upload_amazon_data_csv(uploaded_csv_file)
        return {'amazon_data_uuid': json_uuid}, 201

    @api.expect(amazon_data_csv_update_model)
    def put(self):
        put_payload = request.json
        print(put_payload)
        put_payload = api.payload
        print(put_payload)
        upload_args = upload_parser.parse_args()
        uploaded_csv_file = upload_args['file']  # This is FileStorage instance
        json_uuid = AmazonHandler.upload_amazon_data_csv(uploaded_csv_file, uuid)
        return {'amazon_data_uuid': json_uuid}, 200
RaamEE
  • 3,017
  • 4
  • 33
  • 53
  • Try with importing __request__ from flask and access it with request.json in PUT request handler. – Konrad Dec 23 '18 at 18:51
  • Thanks @KonradSitarz. request.json also returns None. – RaamEE Dec 24 '18 at 06:21
  • @RaamEE how is it. have you manage to get this going? – Slay Sep 25 '19 at 06:30
  • I haven't solved this the best of my memory. Since then I moved to using uvicorn and FastAPI, which I find are better implementations than flask and flask-restplus. I – RaamEE Sep 25 '19 at 08:16

1 Answers1

0

In you code, it was applied two expect to put. In this case, the second expect is ignored even if it is showing on browser. So you are able to get the value of the first expect. As far as I know, it is able to apply two expect in a endpoint.

Venus713
  • 1,441
  • 12
  • 24