gpt4 book ai didi

java - AWS lambda :Malformed Lambda proxy response

转载 作者:行者123 更新时间:2023-12-01 17:53:42 28 4
gpt4 key购买 nike

这是 Lambda 位于 API 网关后面时的一个典型问题。我已经用谷歌搜索了几乎所有的帖子,但仍然无法弄清楚它失败的原因。

我已阅读有关响应应是什么样子的说明 https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format
它正在寻找

{
"isBase64Encoded": true|false,
"statusCode": httpStatusCode,
"headers": { "headerName": "headerValue", ... },
"multiValueHeaders": { "headerName": ["headerValue", "headerValue2", ...], ... },
"body": "..."
}

这是我从 API 网关控制台进行的测试日志(我已仔细检查以确保字段名称完全相同)

Thu Mar 19 10:07:58 UTC 2020 : Endpoint response body before transformations: "{\"statusCode\":200,\"body\":\"RESULTS\",\"isBase64Encoded\":false}"
Thu Mar 19 10:07:58 UTC 2020 : Execution failed due to configuration error: Malformed Lambda proxy response
Thu Mar 19 10:07:58 UTC 2020 : Method completed with status: 502

有人可以帮忙吗?

编辑

我已经从响应中删除了“body”,但仍然遇到相同的错误。以下是实际日志

Thu Mar 19 23:29:54 UTC 2020 : Received response. Status: 200, Integration latency: 24 ms
Thu Mar 19 23:29:54 UTC 2020 : Endpoint response headers: {Date=Thu, 19 Mar 2020 23:29:54 GMT, Content-Type=application/json, Content-Length=48, Connection=keep-alive, x-amzn-RequestId=f2c2c752-a5e0-45e4-9ff0-d91826b51c7b, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-5e740072-46cee9045a56e04b8023816d;sampled=0}
Thu Mar 19 23:29:54 UTC 2020 : Endpoint response body before transformations: "{\"statusCode\":200,\"isBase64Encoded\":false}"
Thu Mar 19 23:29:54 UTC 2020 : Execution failed due to configuration error: Malformed Lambda proxy response
Thu Mar 19 23:29:54 UTC 2020 : Method completed with status: 502

在java应用程序和lambda中捕获的实际JSON响应是

{"statusCode":200,"isBase64Encoded":false}

最佳答案

与 Node.js 示例进行比较后,似乎与处理程序有关。

当使用RequestHandler时,响应是一个字符串

public class MyLambda implements RequestHandler<Object, String> {
@Override
public String handleRequest(Object input, Context context) {

来自API网关的响应如下所示,请注意,整个响应是一个字符串,并且API网关提示

Thu Mar 19 23:29:54 UTC 2020 : Endpoint response body before transformations: "{\"statusCode\":200,\"isBase64Encoded\":false}"
Thu Mar 19 23:29:54 UTC 2020 : Execution failed due to configuration error: Malformed Lambda proxy response

将其更改为 RequestStreamHandler ,错误消失了

public class APIgatewayTest implements RequestStreamHandler {
@Override
public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {

从API网关,响应如下

Thu Apr 09 09:13:08 UTC 2020 : Endpoint response body before transformations: {"statusCode":200,"body":"\"Hello from Lambda!\""}
Thu Apr 09 09:13:08 UTC 2020 : Method response body after transformations: "Hello from Lambda!"

关于java - AWS lambda :Malformed Lambda proxy response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60755047/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com