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