n8jeff
n8jeff

Reputation: 23

POST request from Google Scripts to AWS Lambda coming through as null

I'm attempting to send a POST request from a Google Sheet, containing a given row's data, using Google Apps Script. That request is to an AWS API Gateway, which sends it along to a Lambda function which then does some other things.

The AWS side all works when I use curl to send requests manually, but for some reason when I attempt to do it programmatically inside Sheets, the API will only pass null events to my function. The POST requests appear to be well formed when I print them out on the Sheets side, but my API refuses to pass them through. I know this because I have it set to log all events passed to the function in CloudWatch, and the automatic calls all just print {} as the event.

There's no authentication required in the API, so I can't imagine it being an auth issue. I also saw a common solution being to enable Lambda Proxy Integration, but I don't understand why curl would work if that were the issue.

I built my API using this tutorial almost exactly, if that's helpful.

Here's the relevant bit of code from the Apps Script side:

/**
 * Creates JSON payload and sends request to Amazon. 
 */
function sendRequest()
{
  const url = "https://**********.execute-api.us-east-1.amazonaws.com/prod/DynamoDBManager";

  // Make a POST request with a JSON payload.
  var data = 
  {
    "operation": "create",
    'tableName': 'lambda-apigateway',
    'payload': {
      'Item': {
        'name': nameGiven,
        'contact': contactGiven.toString(),
        'id': currId.toString()
      }
    }
  };

  var options = 
  {
    method: 'post',
    contentType: 'application/json',
    // Convert the JavaScript object to a JSON string.
    body: JSON.stringify(data),
  };
  
  var response =  UrlFetchApp.fetch(url, options);
  var jsonObject = JSON.parse(response.getContentText()); // just makes the response visible in debugger
} 

Upvotes: 2

Views: 841

Answers (1)

Tanaike
Tanaike

Reputation: 201553

In your script, please modify as follows.

From:

body: JSON.stringify(data),

To:

payload: JSON.stringify(data),

Reference:

Upvotes: 1

Related Questions