Flask Upload with no Content-Type or Form Data at All
The solution is to use file streaming. After many days of searching I finally found this blog post which mentions the specific problem of using the Flask/Requests file
call:
The trick seems to be that you shouldn’t use other request attributes like request.form or request.file because this will materialize the stream into memory/file. Flask by default saves files to disk if they exceed 500Kb, so don’t touch file.
Running the attached method immediately solved the issue of upload without multipart-form:
from flask import Flaskimport requestsfrom flask import requestapp = Flask(__name__)# Upload file as stream to a file.@app.route("/upload", methods=["POST"])def upload(): with open("/tmp/output_file", "bw") as f: chunk_size = 4096 while True: chunk = request.stream.read(chunk_size) if len(chunk) == 0: return f.write(chunk) return 'created', 201
(Full credit to @izmailoff, but I added the final line or else there would be a timeout issue on the receivers side)