Search code examples
pythonrestflaskpostmanwerkzeug

Flask - required file is missing


I am playing around with Python, Flask and I am trying to make an endpoint which would accept a file. Basically I want to upload a random .txt/.json file and save it on the server for further processing.

My Problem

I followed the documentation for building it. However, there is no info there like how to build a request to post the data. e.g use multipart/form-data and upload it with a form, or upload it in the body as binary. Anyways, I tried all possible ways in Postman, none of them works, looks like the file just doesn't reach the server. What would be the simplest solution to be able to upload it as binary in the request body? If the code looks fine what should I configure in postman to be able to upload a file??

My endpoint

class RawData(Resource):    
    parser = reqparse.RequestParser()
    parser.add_argument('file',
                        type=werkzeug.datastructures.FileStorage,
                        required=True,
                        help="Required file is missing",
                        location='files')
    
    def post(self):
        data = RawData.parser.parse_args()
        file = data['file']
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        return redirect(url_for('uploaded_file',
                                filename=filename))

Error Message - Postman - HTTP 400

{
    "message": {
        "file": "Required file is missing"
    }
}

enter image description hereenter image description here


Solution

  • Turns out to be a problem with Postman. Still didn't figure out how to post files. However, CURL came in handy and solved the issue with the following cmd:

    curl -v -X POST -H "Content-Type: multipart/form-data" -F "file=@file.txt" http://localhost:5000/api/v1/rawdata/newfile