ReSTfully upload file in Flask without multipart/form-data ReSTfully upload file in Flask without multipart/form-data flask flask

ReSTfully upload file in Flask without multipart/form-data


The issue might be the curl command you are using. The man page recommends --data-binary: "This posts data exactly as specified with no extra processing whatsoever." The --data parameter is a synonym for --data-ascii. Probably don't need the -X parameter then either as it should default to POST.

curl -v -H 'Content-Type: application/octet-stream' -X POST --data-binary @test.zip https://example.com/test/rupload/test.zip

There are additional options on the request.get_data call that could help in case they were overridden somewhere. But looks like that should work on the server side. Disabling the cache feature might benefit your use case in particular.

f.write(request.get_data(cache=False, as_text=False, parse_form_data=False))

If it is the server side you might have to dig deeper into Werkzeug get_input_stream which is what feeds the request object.

It's good that the curl command is using the content-type header as 'application/octet-stream'. Flask doesn't appear to do anything with that in this case, but it can help a more general usage from having the data mangled by proxies or other cases.

Regarding efficiently handling large files, you might want to look at the request stream property, which is what get_data uses internally to read the data from.


Have you tried using hashlib?

import hashlib...@application.route("/rupload/<filename>", methods=['POST', 'PUT'])def rupload(filename):    # Sanity checks and setup skipped.    filename = secure_filename(filename)    fileFullPath = os.path.join(UPLOAD_FOLDER, filename)    file_hash = hashlib.sha256()    with open(fileFullPath, 'wb+') as f:        input = request.get_data()        f.write(input)        file_hash.update(input)        ...    fileDigest = file_hash.hexdigest()