1

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.

1 Answer 1

1

The API documentation shows:

-X GET

You code uses:

fetchParameters.method = "post";

Try changing it to

fetchParameters.method = "get";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.