Chris
Chris

Reputation: 523

How do you pass parameters to a Deployed Google Apps Script API function?

I created a test function (doPost) in a Google Apps Script API using Google Cloud Platform (GCP). I am now trying to call that function from another script in the same project.

I know I am almost there, because this code works:

var token = ScriptApp.getOAuthToken();
var header = {
  "Authorization": "Bearer " + token,
  "function": "doPost",
  "devMode": true,
};
var options = {
  "method": "POST",
  "headers": header,
  "muteHttpExceptions": true,
  "payload": {
    "function": "doPost",
    "devMode": true
  }
};

var url = 'https://script.googleapis.com/v1/scripts/xxxxxxxxxxxxxxxxxxxx:run';

var response = UrlFetchApp.fetch(url, options);

However, when I try to include a parameter in payload above, it no longer works:

  "payload": {
    "function": "doPost",
    "parameters": ['1'],
    "devMode": true
  }

Following other stackoverflow answers, I've tried using in the header:

  "contentType": 'application/json',

And accordingly, for the payload:

 "payload": JSON.stringify({
    "function": "doPost",
    "parameters": ['1'],
    "devMode": true
  })

Whenever I use "JSON.stringify", even without parameters (just like the situation I got to work), it errors out.

With JSON.stringify (and parameters in the payload), I get a worse error, which seems to say it doesn't like any of the payload:

"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"{\"function\":\"doPost\",\"parameters\":[1007],\"devMode\":true}\": Cannot bind query parameter. Field '{\"function\":\"doPost\",\"parameters\":[1007],\"devMode\":true}' could not be found in request message.",
"status": "INVALID_ARGUMENT",
"details": [
  {
    "@type": "type.googleapis.com/google.rpc.BadRequest",
    "fieldViolations": [
      {
        "description": "Invalid JSON payload received. Unknown name \"{\"function\":\"doPost\",\"parameters\":[1007],\"devMode\":true}\": Cannot bind query parameter. Field '{\"function\":\"doPost\",\"parameters\":[1007],\"devMode\":true}' could not be found in request message."

Without JSON.stringify (and with parameters in the payload), I get the error:

"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"parameters\": Cannot bind query parameter. 'parameters' is a message type. Parameters can only be bound to primitive types.",
"status": "INVALID_ARGUMENT",
"details": [
  {
    "@type": "type.googleapis.com/google.rpc.BadRequest",
    "fieldViolations": [
      {
        "description": "Invalid JSON payload received. Unknown name \"parameters\": Cannot bind query parameter. 'parameters' is a message type. Parameters can only be bound to primitive types."

Finally, it doesn't matter what I do with parameters. I'm pretty sure it should be in the format I put above, but I've also tried:

"parameters": [1]
"parameters": 1
"parameters": "1"

among others.

The doPost script is simple for now:

function doPost(parameters) {
  Logger.log('parameters = ' + parameters);
  return "Hello";
}

Here is the stackoverflow question that seems to be most like this: Apps Script API returning 404 error for existing project. Error returned as HTML rather than JSON, but doesn't seem to answer my problem.

I've studied the scripts.run page about parameters: https://developers.google.com/apps-script/api/reference/rest/v1/scripts/run#authorization-scopes, along with many other pages, including the URL Fetch Service: https://developers.google.com/apps-script/reference/url-fetch.

This is certainly not my first time using UrlFetchApp in Google Apps Script, but it is when calling my own GAS API.

Any help would be greatly appreciated!

Thanks!

Upvotes: 0

Views: 2280

Answers (1)

Chris
Chris

Reputation: 523

Not long after I posted this, and after continuing to see post after post say that you should use JSON.stringify for the payload and contentType: application/json in the header, in the header I changed:

"contentType": "application/json"

to

"Content-Type": "application/json"

and it works now!

Upvotes: 0

Related Questions