BookmarkSubscribeRSS Feed
Chuck_IV4
Obsidian | Level 7

Hi Everyone,

  I am really struggling with converting what I am able to get to run in Postman, into a valid Proc HTTP statement...

 

I usually am able translate the cURL code snippets from Postman into SAS using Proc HTTP quite easily, but this one is stumping me.

 

Here is the cURL code that works(removed my secret code and Postman token)...

curl -X GET \
  'https://mixpanel.com/api/2.0/jql?script={{encoded_script}}' \
  -H 'Authorization: Basic <my Secret Code>' \
  -H 'Postman-Token: My Token' \
  -H 'cache-control: no-cache'

 

The {{encoded_script}} is a prerequest script that URLEncodes the actual script...

var encoded = encodeURIComponent(function main() {return Events({from_date: "2018-10-17", to_date: "2018-10-19"})});
postman.setEnvironmentVariable("encoded_script", encoded);

 

In reading the 3rd party vendors API documentation(JQL is what they use), here is a cURL example they give...

curl https://mixpanel.com/api/2.0/jql \
    -u Secret_Code \
    --data-urlencode params='{"from_date":"2016-01-01", "to_date": "2016-01-07"}' \
    --data-urlencode script='function main(){ return Events(params).groupBy(["name"], mixpanel.reducer.count()) }'

 

My Proc HTTP statement looks like this, where the script= part has been run through the URLEncode function(I tried it not running it through that as well)...

 

proc http
    method="GET"
    url="https://mixpanel.com/api/2.0/jql?script='%27function%20main()%20%7Breturn%20Events(%7Bfrom_date%3A%2...'"
    ct="application/json"
    headerout=hresult
    out=json;
    headers "Authorization" = "Basic &api_secret.";
  run;

 

It authenticates fine but I always get a Bad Request with a message similar to ...

{"request": "/api/2.0/jql?script=%27function%20main()%20%7Breturn%20Events(%7Bfrom_date%3A%20%222018-10-17%22%2C%20to_date%3A%20%222018-10-19%22%7D)%7D%27", "error": "[Validate failed]the query must define the main() function\n"}

 

I have also tried putting the script= into the IN= statement but the API always tells me I am missing the Script= statment(which is required).

 

It seems to have something to do with URL Encoding the script, but it still doesn't seem like it likes my URL Encoded text.

 

Any help would be appreciated.

1 REPLY 1
Chuck_IV4
Obsidian | Level 7

Ok, I am very close to getting this working. If I remove the single quotes around the URL Encoded script= code, it WORKS(See below)!

 

proc http
    method="GET"
    url="https://mixpanel.com/api/2.0/jql?script=function%20main()%20%7Breturn%20Events(%7Bfrom_date%3A%20%22..."
    ct="application/json"
    headerout=hresult
    out=json;
    headers "Authorization" = "Basic &api_secret.";
  run;

 

The issue was the script = code does NOT need the single quotes(even though the cURL statement shows them).

 

However, this now presents another problem. I need to put the script code into its own file and then call the file. The problem is, when I put the file name as the script= statement(something like script=jscript.js), JQL thinks the jscript.js the actual script code and not a file name.

 

I guess I can set a macro variable to the entire file and then just drop the macro variable in place of the jscript.js filename but that seems a little odd to have to do.

 

Any thoughts on this one?

 

Thanks.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 906 views
  • 0 likes
  • 1 in conversation