amazon-web-services - 由于配置错误 : Malformed Lambda proxy response while sending back nested JSON value 执行失败

标签 amazon-web-services aws-lambda aws-api-gateway

我有一个 lambda 函数,它从 dynamdb 表返回一个值。该值采用嵌套 jason 格式。

当我在它自己的控制台中测试 lambda 时,它工作正常。 但 当我将它与 API 网关集成时,它给我一个内部服务器错误并显示此消息。

Execution failed due to configuration error: Malformed Lambda proxy response

这是我的回复正文

 return {
            "isBase64Encoded": "true",
            "statusCode": 200,
            "headers": { "Access-Control-Allow-Methods": "*", "Access-Control-Allow-Headers": "*", "Access-Control-Allow-Origin": "*" },
            "body": response
        }

有人知道这个问题吗?

我尝试过

return {
            "isBase64Encoded": "true",
            "statusCode": 200,
            "headers": { "Access-Control-Allow-Methods": "*", "Access-Control-Allow-Headers": "*", "Access-Control-Allow-Origin": "*" },
            "body": json.dumps(response['Items'])
        }

它似乎正在工作。

是什么问题导致的?因为如果我有一个普通的 JSON。

"body": response

工作正常。仅当存在嵌套 JSON 时才会出现问题

这是 JSON 对象:

[
   {
      "time_stamp":"2021-01-13 06:02:42",
      "broker_id":"broker1",
      "load_id":"ab6fd05f-3f54-44dc-ae6d-28e924fe1ae2",
      "messages":[
         {
            "date":"22:32",
            "messanger_id":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e6d6f7c7c676b7c4e6f6c6d206d6163" rel="noreferrer noopener nofollow">[email protected]</a>",
            "message":"Hello",
            "user":"carrier"
         },
         {
            "date":"22:35",
            "messanger_id":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ccaebea3a7a9be8cadaeafe2afa3a1" rel="noreferrer noopener nofollow">[email protected]</a>",
            "message":"Hi",
            "user":"broker"
         }
      ],
      "carrier_id":"carrier1"
   },
   {
      "time_stamp":"2021-01-13 06:03:32",
      "broker_id":"broker1",
      "load_id":"ab6fd05f-3f54-44dc-ae6d-28e924fe1ae2",
      "messages":[
         {
            "date":"22:32",
            "messanger_id":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d1b2b0a3a3b8b4a391b0b3b2ffb2bebc" rel="noreferrer noopener nofollow">[email protected]</a>",
            "message":"Hello",
            "user":"carrier"
         },
         {
            "date":"22:35",
            "messanger_id":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6a4b4a9ada3b486a7a4a5e8a5a9ab" rel="noreferrer noopener nofollow">[email protected]</a>",
            "message":"Hi",
            "user":"broker"
         }
      ],
      "carrier_id":"carrier2"
   }
]

最佳答案

它可以与 json.dumps 配合使用,因为您正在获取 JSON 字符串,这是代理 lambda 集成所必需的。来自 docs :

With the Lambda proxy integration, the Lambda function must return output of the following format:

{
    statusCode: "...",            // a valid HTTP status code
    headers: { 
        custom-header: "..."      // any API-specific custom header
    },
    body: "...",                  // a JSON string.
    isBase64Encoded:  true|false  // for binary support
}

如您所见,body 中必须使用 JSON 字符串。您无法像编写 "body": response['Items']

时那样返回纯 json 对象

关于amazon-web-services - 由于配置错误 : Malformed Lambda proxy response while sending back nested JSON value 执行失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65697024/

相关文章:

node.js - 是否有部署架构以使用微服务模型发送 SMS 的最佳方法?

python - 如何通过 Boto3 将文件上传到 S3 中的特定文件夹

python - 如何使用 boto 检索 ec2-image 的状态?

javascript - S3 : Allow download of files for those who have some token

aws-lambda - 如果 VTL/API 网关模板中不存在 key,则返回 null

python - 使用 aws 提供动态网页?

java - 来自 AdminInitiateAuth 的 ID token 对于 API 网关调用无效

php - Amazon Product API 检索所有产品

node.js - 无服务器嵌入式堆栈创建失败

aws-api-gateway - CloudFormation堆栈资源依赖性问题: API Gateway Deployment + UsagePlan