python3x upload_file error

  • 1
  • Question
  • Updated 5 days ago
  • (Edited)
Hello,
I am working with the Citrix ShareFile API Documentation for Python.
I am using Python 3.7 (it might be an important element since I had to make a couple of adjustments to the script to get the token).

Changes made (because module doesn't exist in Py3):
L2: import http.client as httplib <= import httplib
L6 import urllib.parse as urlparse <= instead of import urlparse
L68: urlparse.urlencode(params) <= urllib.urlencode(params)

Now I am looking at the function "upload_file" and I got several errors along the way. I changed the function in order to make it work (code 200) but as I read in the documentation, this code only say that the request was made, but it can be an empty request (which means no file is actually been uploaded).

Anyone has used this API for file upload using python3x?

Below are the errors I was getting using the functions available (https://api.sharefile.com/rest/samples/python.aspx) and how I made it work.

Error1:
L294 data_str = newline.join(data)<br title="" type="" name="" value="" alt="" target="" rel="">TypeError: sequence item 4: expected str instance, bytes found

Solution1:

    newdata = []
    for i in data:
        if isinstance(i, bytes):
            newdata.append(str(i,encoding="utf8", errors='ignore'))
        else:
            newdata.append(i)
    data_str = newline.join(newdata)

Error2:
L303 http.send(data_str)<br title="" type="" name="" value="" alt="" target="" rel="">TypeError: memoryview: a bytes-like object is required, not 'str'<br title="" type="" name="" value="" alt="" target="" rel=""><br title="" type="" name="" value="" alt="" target="" rel="">Solution2:<br title="" type="" name="" value="" alt="" target="" rel=""><br title="" type="" name="" value="" alt="" target="" rel="">&nbsp;&nbsp;&nbsp; data_str = base64.encodebytes(data_str.encode())<br title="" type="" name="" value="" alt="" target="" rel="">&nbsp;&nbsp;&nbsp; http.send(data_str)
Photo of renaud

renaud

  • 3 Posts
  • 0 Reply Likes
  • thankful

Posted 6 days ago

  • 1
Photo of renaud

renaud

  • 3 Posts
  • 0 Reply Likes
I do not know what is happening to the chat but it seems confused and returning weird html code...
Photo of renaud

renaud

  • 3 Posts
  • 0 Reply Likes
Found the response.