flask rest api upload image 1

flask rest api upload image

    from flask import Flask
    from flask_restful import Resource, Api, reqparse
    import werkzeug

    class UploadAudio(Resource):
      def post(self):
        parse = reqparse.RequestParser()
        parse.add_argument('file', type=werkzeug.datastructures.FileStorage, location='files')
        args = parse.parse_args()
        audioFile = args['file']

Here is what the above code is Doing:
1. We are creating a class called UploadAudio which inherits from the Resource class.
2. We are creating a post method which will be called when a POST request is made to the API.
3. We are creating a RequestParser object which will help us parse the request.
4. We are adding an argument called file which is of type FileStorage. This argument will be present in the files section.
5. We are parsing the request using the RequestParser object.
6. We are saving the file to the disk.

Now, we need to add this resource to our API.

Similar Posts