1

this is a follow on from this question

Something is wrong with my saqlquery string variable I am passing as 'data' to the api.

The below code is returning the response

{"errorCode":"118","message":"The request body is empty"}

Python code below

import json, requests


consumer_key = "xxxx"
consumer_secret = "xxxx"
username = "xxxx"
password = "xxxx"

payload = {
    'grant_type': 'password',
    'client_id': consumer_key,
    'client_secret': consumer_secret,
    'username': username,
    'password': password
}

r = requests.post("https://login.salesforce.com/services/oauth2/token",
    headers={"Content-Type":"application/x-www-form-urlencoded"},
    data=payload)


print r.content
body = json.loads(r.content)
token = body["access_token"]
saqlquery = "q = load \"0Fb28000000PBfqCAG/0Fc28000000Ag0nCAC;\";q = group q by 'Name';q = foreach q generate 'Name' as 'Name', sum('Amount') as 'sum_Amount';}"
url = body["instance_url"] + "/services/data/v36.0/wave/query"

r = requests.post(url, headers = {"Authorization":"Bearer " + token}, data = {"query":saqlquery});

print r.content
1
  • add print {"query":saqlquery} and compare result to your saql (you have extra symbols) May 7, 2016 at 4:16

2 Answers 2

1

If you print your JSON request:

print {"query" : saqlquery}

You will get python String:

{
    'query': 'q = load "0Fb28000000PBfqCAG/0Fc28000000Ag0nCAC;";
    q = group q by \'Name\';
    q = foreach q generate \'Name\' as \'Name\', sum(\'Amount\') as \'sum_Amount\';}'
}

When example should be JSON:

{
    "query":"q = load \"0Fbxx0000000006CAA/0Fcxx000000001dCAA\";
     q = group q by 'FirstName';
     q = foreach q generate 'FirstName' as 'FirstName', count() as 'count';
     q = limit q 2000;"
}

Try to load your String into JSON:

msgStr = {
    'query':'q = load \"0Fbxx0000000006CAA/0Fcxx000000001dCAA\";'
        +'q = group q by \'FirstName\';'
        +'q = foreach q generate \'FirstName\' as \'FirstName\', count() as \'count\';'
        +'q = limit q 2000;'
}

msgJson = json.dumps(msgStr)
print msgJson

Result is same as example.

Finally send request:

headers = {"Authorization":"Bearer " + token,"Content-Type":"application/json"}
r = requests.post(url, headers = headers, data = msgJson);
print r.content

I dont have access to wave(its stuck), but I've checked similar String with SF rest api, please add any issues if you have

2
  • Ok excellent, thank you - I don't know the syntax for the below: " r = requests.post(url, headers = {"Authorization":"Bearer " + token}, data = {msgJson}); " Is that how it should be passed? I'm getting an ugly error which ends with "TypeError: 'set' object has no attribute 'getitem'"
    – Dawid
    May 8, 2016 at 1:19
  • dont miss header and set data = msgJson May 8, 2016 at 2:47
0

I know this question has been inactive for a while but I wanted to share something I have been working on which makes SAQL queries in python a bit easier. I have put together a python package that can be used to easily run a SAQL query so that you can explore further in python. Here is an example of how the package would work. It uses a live session cookie so you would need to be logged into Einstein Analytics from your browser.

import SalesforceEinsteinAnalytics as EA
EA = EA.salesforceEinsteinAnalytics(env_url='https://yourinstance.my.salesforce.com', browser='chrome')

saql = '''
q = load "DatasetAPIName";
q = filter q by date('data_dt_Year', 'data_dt_Month', 'data_dt_Day') in ["current month".."current month"];
q = group q by ('dimension1', 'dimension2');
q = foreach q generate 'dimension1', 'dimension2', sum('metric') as 'metric', unique('id') as 'id_count';
q = order q by 'metric' desc;
q = limit q 2000;
'''

result = EA.run_saql_query(saql=saql)
print(result.head())

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .