I am pretty new to Google Apps Script. I am trying to create a script to pull data from a public API. I need to pass parameters, but can't figure out how to do it in Apps Script
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>" \
-d '{"name":"My Lead","email":{"email":"[email protected]","category":"work"}}'
I have this function setup in Apps Script:
function myFunction() {
var url = "https://api.prosperworks.com/developer_api/v1/leads/search";
var fetchParameters = {};
fetchParameters.method = "post";
fetchParameters.contentType = "application/json";
fetchParameters.headers = {
"X-PW-AccessToken": <TOKEN>,
"X-PW-Application": "developer_api",
"X-PW-UserEmail": <USER_EMAIL_ADDRESS>
};
fetchParameters.muteHttpExceptions = true;
fetchParameters.payload = {"name":"My Lead","email":{"email":"[email protected]","category":"work"}};
var response = UrlFetchApp.fetch(url, fetchParameters);
var dataAll = JSON.parse(response.getContentText());
}
I added fetchParameters.payload because that appeared to be how to pass the values, but I get this error upon parsing the JSON:
Execution failed: SyntaxError: Unexpected token: <
If I comment out fetchParameters.payload it works successfully, but doesn't factor in any parameters (obviously).
I have tried: fetchParameters.d, fetchParameters.data, and many others, but nothing seems to work.