Reputation: 964
I receive a file from a HTML form in Flask and want to post it to another service using Requests. In the destination service, the incoming request doesn't contain the file. How do I post an uploaded file?
f = request.files['file']
sendFile = {"file": FileStorage(filename=f.filename, stream=f.stream, content_type=f.content_type, content_length=actualSize)}
c = checksumMD5(f.stream)
r = requests.post("http://myservicedotcom/upload", files=sendFile,
headers={"X-Auth-Token":token, "Checksum":checksumMD5(f.stream), "File-Size":actualSize})
Upvotes: 7
Views: 4051
Reputation: 1121844
You do not need to wrap your uploaded file in a FileStorage
instance; that's an implementation detail of Werkzeug (the library underpinning Flask).
Instead, you need to rewind your stream after creating a checksum:
f = request.files['file']
c = checksumMD5(f.stream)
f.seek(0)
sendFile = {"file": (f.filename, f.stream, f.mimetype)}
r = requests.post("http://myservicedotcom/upload", files=sendFile,
headers={"X-Auth-Token": token, "Checksum": c, "File-Size": actualSize})
Upvotes: 14