1

How do I save the data uploaded using a curl command like curl localhost:5000/upload/test.bin --upload-file tmp/test.bin in a Flask-RESTful PUT handler method?

The code in Ron Harlev's answer to Flask-RESTful - Upload image works with the POST request from curl -F "file=@tmp/test.bin" localhost:5000/upload/test.bin (slightly modified below):

def post(self, filepath):
    parse = reqparse.RequestParser()
    parse.add_argument('file', type=werkzeug.datastructures.FileStorage, location='files')
    args = parse.parse_args()
    upload_file = args['file']
    upload_file.save("/usr/tmp/{}".format(filepath))
    return ({'filepath': filepath}, 200)

But if I try to use the code to handle the PUT request from curl --upload-file (changing post to put above, of course) I get: "'NoneType' object has no attribute 'save'". This refers to the second-last line in the above code.

How do I get a handle to the file data uploaded using curl --upload-file, so I can save it to a local file?

Update: This works around the problem: curl --request PUT -F "file=@tmp/test.bin" localhost:5000/upload/test.bin, but I still don't have an answer to my question.

Community
  • 1
  • 1
Daryl Spitzer
  • 143,156
  • 76
  • 154
  • 173

1 Answers1

0

curls docs define --upload-file as a PUT http request https://curl.haxx.se/docs/manpage.html#-T.

I'm not sure of the need to handle this through a restful API and I'm pretty sure that's where curl is causing problems, perhaps the assumption that this must be done through flask-restful is holding you back?

maybe try building this as a vanilla flask endpoint instead, this code should work for you.

from flask import Flask, request, jsonify
...

@app.route('/simpleupload/<string:filepath>', methods=['POST','PUT'])
def flask_upload(filepath):
    with open("/tmp/{}".format(filepath), 'wb') as file:
        file.write(request.stream.read()) 
    return (jsonify({'filepath': filepath}), 200)
BenDog
  • 366
  • 1
  • 13