我正在使用AWS Lambda函数来处理来自AWS API Gateway调用的请求。我正在根据请求发送负载,并且可以在CloudWatch中验证负载是否已从网关传递给lambda函数。但是,请求的正文在我的Lambda函数中为空。
我看了这个问题:AWS Lambda Go function not getting request body when called via API GW
我正在尝试使用以下https://github.com/aws/aws-lambda-go/blob/master/events/apigw.go库在此处复制答案,但是我仍然无法获取请求正文。
这是我的Lambda代码:
package main
import (
"context"
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
func handleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
fmt.Println("Body")
fmt.Println(request.Body)
fmt.Printf("Processing request data for request %s.\n", request.RequestContext.RequestID)
fmt.Printf("Processing request data for request %s.\n", request.RequestContext.RequestID)
fmt.Printf("Body size = %d.\n", len(request.Body))
fmt.Println("Headers:")
for key, value := range request.Headers {
fmt.Printf(" %s: %s\n", key, value)
}
return events.APIGatewayProxyResponse{Body: request.Body, StatusCode: 200}, nil
}
func main() {
lambda.Start(handleRequest)
}
我希望在Cloudwatch中看到“正文”之后的一些数据,但是什么都没有。
答案 0 :(得分:0)
原始问题中的代码正确。 handleRequest的第二个参数的类型为APIGatewayProxyRequest。在API Gateway中,我发送的是普通请求,而不是代理请求。我将API网关路由重新部署为代理请求,并获得了我期望的请求正文。我仍然不确定我的原始请求是否发送失败,还是传递给handleRequest函数的普通请求的结构与代理请求的结构不同,因此APIGatewayProxyRequest类型无法解析其主体