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})
Mainly because GET is used to get information, while POST is used to posting it to the server. Very basically, use GET only for things that do not change anything on the server, and POST only for things that do. Uploading data in a GET command makes perfect sense when the data is a parameter for the GET.
In your OneDrive, select the folder where you want the files to be uploaded, and then select Request files. Under What files are you requesting, enter a descriptive name for the files you are requesting from others. (They'll see this description when they get the file request.)
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})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With