Happy Business Starts Here

Re: Data Query Post using API in python

Highlighted
Valued Scholar

Data Query Post using API in python

Hello All , Can any one suggest how I can post a data query using python and rest API and download data as CSV. I'm getting 400 bad request error when I try to run below sample 

 


import httplib2,json,http.client,urllib.request,csv,time,os,datetime,sys,logging,subprocess

logging.basicConfig(level=logging.INFO)

logger = logging.getLogger(__name__)

httplib2.debuglevel = 1
h = httplib2.Http('.cache', disable_ssl_certificate_validation=True)
login_headers = {"Content-Type": "application/json",
"apiAccessKeyId": 'username',
"apiSecretAccessKey": 'pwd',}

services_login_url = "https://rest.zuora.com/"
resp, content = h.request(services_login_url, "POST", headers=login_headers)
cookie = resp["set-cookie"]

export_excel_headers = {"Content-Type": "application/json",
"Cookie": cookie}

create_export_excel_url = "https://rest.zuora.com/query/jobs"
createexp = "{\n \"Format\": \"csv\", \n \"Name\": \"test\", \n \"Query\": \"SELECT accountnumber, balance FROM Account WHERE Account.balance > 100 \"\n}"

resp, content = h.request(create_export_excel_url, "POST",createexp , headers=export_excel_headers)


data = json.loads(content)
export_id = data.get("Id");

 

Thanks

 

1 REPLY 1
Highlighted
Zuora Staff
Zuora Staff

Re: Data Query Post using API in python

First, I recommend the Requests library https://requests.readthedocs.io/en/master/.

 

With Data Query, you need to do the following: