Bulk API curl request in Python

  • 1
  • Problem
  • Updated 3 months ago
  • Solved
Hi all,

we'll upload usage data with a bulk api job to automate our process.

We transform our usage data in a python project but we are not able to create a cURL request in it to upload the data.

Did anyone of you tranfered the (BULK API) cURL request to python?

If yes I also can share our ideas.

Thanks,
Christian
Photo of Christian Auner

Christian Auner

  • 164 Points 100 badge 2x thumb

Posted 4 months ago

  • 1
Photo of Kunal Bhat

Kunal Bhat, Employee

  • 960 Points 500 badge 2x thumb
Hi Christian,


Here's an example of a script converting the Bulk API cURL request to Python. It uses the 'requests' module and requests_toolbelt which is an extension of the requests module.


Original Bulk API cURL:

curl -v -X POST -H "Content-Type: multipart/form-data" -H "loginName: loginName" -H "appOrgId:xxxxOrgID" -H "accessKey:xxxxKey" --form "file=@/location/TestFile.csv" --form 'jobId=xxxx' -k https://url/v1.0/admin/connector/job/bulkimport


Python script:

###############

import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
multipart_data = MultipartEncoder(
    fields={
            'file': ('TestFile.csv', open('/location/TestFile.csv', 'rb'), 'text/csv'),
            'jobId': 'xxxx'
           }
    )
url = 'https://url/v1.0/admin/connector/job/bulkimport'
headers = {'content-type': multipart_data.content_type, 'loginName': 'loginName', 'appOrgId': 'xxxxOrgID', 'accessKey': 'xxxxKey'}
request = requests.post(url, data=multipart_data, headers=headers)
print request.text


Is this what you were looking for? 



Thanks,
Kunal
Photo of Christian Auner

Christian Auner

  • 164 Points 100 badge 2x thumb
Thanks Kunal,

this is exactly what I'm looking for.

Thanks alot.
Best,

Christian
Photo of Kunal Bhat

Kunal Bhat, Employee

  • 960 Points 500 badge 2x thumb
Great, glad to help!