Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps Script cUrl request

I am pretty new to Google Apps Script. I am trying to create a script to pull data from a public API.

The API documenation indicates that the information can be accessed like this:

curl -H "Content-Type: application/json" -X GET 
--header "X-PW-AccessToken:<TOKEN>" \
--header "X-PW-Application:developer_api" \
--header "X-PW-UserEmail:<USER_EMAIL_ADDRESS>" \
https://api.prosperworks.com/developer_api/v1/leads/2

I have this function setup in Apps Script:

function myFunction() {
  var url = "https://api.prosperworks.com/developer_api/v1/leads/2";
  var headers = {
             "contentType": "application/json",
             "headers":{"X-PW-AccessToken": "<TOKEN>",
                        "X-PW-Application": "developer_api",
                        "X-PW-UserEmail": "<USER_EMAIL_ADDRESS>"}
             };

  var response = UrlFetchApp.fetch(url, headers);
  var text = response.getResponseCode();
  Logger.log(text);
}

When I run the code, the response appears to be empty but text is 200.

enter image description here

I am able to make a call in Postman and get the expected JSON object.

What am I doing wrong in Apps Script? How do I access the JSON object?

like image 856
davids Avatar asked Aug 13 '16 01:08

davids


1 Answers

Without having code to test, it is hard to know for sure, but it looks like you just need to parse your JSON

Try adding:

var data = JSON.parse(response.getContentText());
like image 111
user2639740 Avatar answered Oct 16 '22 15:10

user2639740