restpython-requestszfs

Uploading binary file to API using python


I am trying to upload a package binary over to the RestAPI of storage using python. But it keeps throwing error and couldn't upload the file. Below is the code I am using to achieve it :

jsonheaderup={'Content-Type': 'application/octet-stream'}

file = open('install.pkg.gz', 'rb')
files = {'file': file}

def upload_code():

  u = requests.post("%s/api/sys/v2/updates" % (url), files=files, verify=False,      headers=jsonheaderup)
  l = json.loads(u.text)

upload_code()

Solution

  • The earlier posts didn't really helped but I figured it out by referring the original doc of requests: Streaming uploads. Check doc here.

    As my file was huge around 1.9 GB, so session was breaking in between the upload process, hence giving error as "Internal error".

    As its huge file I streamed and upload it by providing a file-like object in my function:

    def upload_code():
      jsonheaderup={'Content-Type': 'application/octet-stream'}
      with open('/root/ak-nas-2013-06-05-7-18-1-1-3-nd.pkg.gz', 'rb') as file:
        requests.post("%s/api/system/v1/updates" % (url), data=file, auth=zfsauth, verify=False, headers=jsonheaderup, timeout=None)