Python file upload - requests adds Content-* to files
AnsweredI'm writing a small Python library to upload files and attach them to tickets.
The file does get uploaded, however it looks like Python requests is adding some of the HTTP data into the file. This is best seen with just plain text files.
Original file:
cat /tmp/file1
file 1
now with more things in it
After it has been uploaded and downloaded from the content_url returned from the POST request:
cat ~/Downloads/file1
--d0fc8ad134ce4ffab253f2aba200458e
Content-Disposition: form-data; name="/tmp/file1"; filename="file1"
Content-Type: application/binary
file 1
now with more things in it
--d0fc8ad134ce4ffab253f2aba200458e--
I've tweaked a whole bunch of settings but not seen any change. Is anyone able to tell me why this happens? Uploading with a standard curl does not cause this issue.
Code is below:
def upload_file(self, file_list):
self.token = ""
for file in file_list:
_filename = file.split("/")[-1]
_filedict = {file : (_filename, open(file, 'rb'), "application/binary")}
self.url = "https://{0}.zendesk.com/api/v2/uploads.json?" \
"filename={1}&token={2}".format(self.baseurl,
_filename,
self.token)
status = requests.post(self.url, auth=self.auth, files=_filedict)
if status.status_code != 201:
return False, status
self.token = status.json()["upload"]["token"]
import ipdb; ipdb.set_trace()
return True, self.token
-
Official comment
One more thing, Scott-
Using the following I received the appropriate JSON response, given me a token which could then be used to attach your upload to another object such as a ticket, etc.
#!/usr/bin/python
import sys
import requests
user = 'email@example.com/token'
token = '{API_TOKEN}'
url = 'https://{SUBDOMAIN}.zendesk.com/api/v2/uploads.json?filename={foo}'
headers = {'Content-type': 'application/binary', 'Accept': 'text/plain'}
uploaddata = '{foo}'
s = requests.session()
s.auth = (user, token)
resp = s.post(url, headers = headers, data = uploaddata )
print(resp.content)Certainly not production ready, but my test file was not overwritten as was in your case. Please give it a try!
-
Hi there Scott-
Thanks for writing in. Given that this is not occurring with cURL, I would consider it unlikely Zendesk is writing this data to your file(s). I wanted to ask if you had seen the Zenpy Client Library. While not supported by us, the developer maintains it and is quite well-documented.
Take care!
-
Hi Joseph,
Thanks for having a look. I 100% agree this is a Python problem, not a Zendesk one. :-)
I had seen the Zenpy module, but honestly I wanted to try this myself and I don't believe it supported the OAuth2 authentication I've implemented in my app now.
I see you're uploading the contents of the file using the data kwarg, not the files kwarg which I believe is normal to use with the Python requests module. Is there a particular reason for this?
Scott
-
Oh... For the record, the following code does work successfully.
def upload_file(self, file_list):
self.token = ""
self.headers["Content-Type"] = "application/binary"
for file in file_list:
_filename = file.split("/")[-1]
self.url = "https://{0}.zendesk.com/api/v2/uploads.json".format(self.baseurl)
self.params = {"filename" : _filename, "token" : self.token}
status = requests.post(self.url, headers=self.headers, data=open(file, 'rb').read(), params=self.params)
self.logger.debug(pretty_print_POST(status.request))
if status.status_code != 201:
return False, status.text
self.token = status.json()["upload"]["token"]
return True, self.token -
Scott - this saved me today. Thank you!!
Please sign in to leave a comment.
5 Comments