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
.
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?
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With