簡體   English   中英

從 Google Scripts 到 AWS Lambda 的 POST 請求通過 null

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

我正在嘗試使用 Google Apps 腳本從包含給定行數據的 Google 表格發送 POST 請求。 該請求發送到 AWS API 網關,該網關將其發送到 Lambda function 然后執行其他一些操作。

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. 當我在表格一側打印 POST 請求時,它們的格式似乎很好,但我的 API 拒絕通過它們。 我知道這一點是因為我已將其設置為記錄傳遞給 CloudWatch 中 function 的所有事件,並且自動調用都只是打印 {} 作為事件。

API 不需要身份驗證,所以我無法想象這是一個身份驗證問題。 我還看到一個常見的解決方案是啟用 Lambda 代理集成,但我不明白為什么 curl 會起作用,如果這是問題。

如果有幫助的話,我幾乎完全按照本教程構建了我的 API。

以下是 Apps 腳本方面的相關代碼:

/**
 * 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
} 

在您的腳本中,請進行如下修改。

從:

body: JSON.stringify(data),

至:

payload: JSON.stringify(data),

參考:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM