Sarang Sangram
Sarang Sangram

Reputation: 71

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

Upvotes: 2

Views: 7489

Answers (2)

Sarang Sangram
Sarang Sangram

Reputation: 71

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)

Upvotes: 3

carapaece
carapaece

Reputation: 351

At first glance, I can not see any mistake.

Did you see this: Python 3 script to upload a file to a REST URL (multipart request) ?

Upvotes: 0

Related Questions