I have two REST (Flask) APIs. The first API reads a zip file from the local disk, and returns it in the POST request. The second API calls the first API, and writes the file onto local.
Being a bit ignorant of encodings, I'm running into issues.
@app.route('/push/', methods=['POST'])
def push():
with open('/path/to/zip_file.zip', 'r') as f:
foo = f.read()
return foo
@app.route('/pull/', methods=['POST'])
def pull():
url = 'https://myhost.com/push/'
r = requests.post(url, verify=False)
with open(/new/path/to/zip_file.zip', 'w') as f:
# f.write(r.text) # this fails with UnicodeEncodeDecode error
f.write(r.text.encode('utf-8')) # this doesn't fail, but I can't unzip the resulting file
In the second API, I originally tried to f.write(r.text)
, but this failed with:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 63-64: ordinal not in range(128)
I tried changing it to f.write(r.text.encode('utf-8'))
after Googling, and while the writing of the file then worked, I received the following error while attempting to unzip
it in Linux:
error [OWBMaster_0.1.zip]: missing 3112778871 bytes in zipfile
(attempting to process anyway)
error [OWBMaster_0.1.zip]: start of central directory not found;
zipfile corrupt.
(please check that you have transferred or created the zipfile in the
appropriate BINARY mode and that you have compiled UnZip properly)
Is there a trick to sending zip files over REST with Requests?