Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post uploaded file using requests

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})
like image 855
urb Avatar asked Sep 08 '15 14:09

urb


People also ask

Can we upload file using GET request?

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.

How do I use file request?

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.)


1 Answers

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})
like image 52
Martijn Pieters Avatar answered Sep 28 '22 19:09

Martijn Pieters