douyouzheng2209
2019-08-05 14:55
浏览 75
已采纳

API网关请求中的GoLang AWS Lambda函数缺失主体

I'm using an AWS Lambda function to handle a request from an AWS API Gateway call. I'm sending a payload on the request, and I can verify in CloudWatch that the payload is being passed from the gateway to the lambda function. However, the body of the request is null inside my Lambda function.

I looked at this question: AWS Lambda Go function not getting request body when called via API GW

I am trying to replicate the answer there by using this library: https://github.com/aws/aws-lambda-go/blob/master/events/apigw.go, but I'm still not able to get the request body.

Here is my Lambda code:

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.
", request.RequestContext.RequestID)

    fmt.Printf("Processing request data for request %s.
", request.RequestContext.RequestID)
    fmt.Printf("Body size = %d.
", len(request.Body))

    fmt.Println("Headers:")
    for key, value := range request.Headers {
        fmt.Printf("    %s: %s
", key, value)
    }

    return events.APIGatewayProxyResponse{Body: request.Body, StatusCode: 200}, nil
}

func main() {
    lambda.Start(handleRequest)
}

I'm expecting to see some data after "Body" in Cloudwatch, but there is nothing.

图片转代码服务由CSDN问答提供 功能建议

我正在使用AWS Lambda函数来处理来自AWS API Gateway调用的请求。 我正在根据请求发送负载,并且可以在CloudWatch中验证负载是否已从网关传递给lambda函数。 但是,在我的Lambda函数中,请求的主体为null。

我看了一个问题:

我正在尝试使用以下库在此处复制答案: https://github.com/aws/aws-lambda-go/blob/master/events/apigw.go ,但我仍然无法获取请求正文。

这是我的Lambda代码:

 包main 
 
import(
“上下文” 
“ 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,错误){
 
 fmt.Println(“ Body”)
 fmt.Println(request.Body)
 
 fmt.Printf(“ Proce  ssing请求%s的请求数据。
“,request.RequestContext.RequestID)
 
 fmt.Printf(”处理请求%s。
的请求数据,request.RequestContext.RequestID)
 fmt.Printf  (“ Body size =%d。
”,len(request.Body))
 
 fmt.Println(“ H​​eaders:”)
表示键,值:=范围请求。Headers{
 fmt.Printf  (“%s:%s 
”,键,值)
} 
 
返回事件。APIGatewayProxyResponse{正文:request.Body,状态代码:200},nil 
} 
 
func main(){  
 lambda.Start(handleRequest)
} 
   
 
 

我希望在Cloudwatch中看到“正文”之后的一些数据,但是什么都没有。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douye9175 2019-08-06 12:48
    已采纳

    The code in the original question is correct. The second argument to the handleRequest is of the type APIGatewayProxyRequest. In API Gateway, I was sending a normal request, not a proxy request. I redeployed my API Gateway route as a proxy request and got the request body I was expecting. I'm still not really sure whether my original request was failing to send the body, or if the structure of a normal request passed into the handleRequest function is different from that of a proxy request and so the APIGatewayProxyRequest type was unable to parse its body.

    打赏 评论

相关推荐 更多相似问题