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

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.

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥15 报错问题,只有两个错误,如图片所示,i多次初始化
    • ¥15 matlab runtime 如何收费
    • ¥20 用C语言绘制cs1.6方框透视 出现了点问题 绘制不上去 矩阵数据 和敌人坐标都是正确的
    • ¥15 Tpad api账户 api口令
    • ¥30 ppt进度条制作,vba语言
    • ¥15 生信simpleaffy包下载
    • ¥15 在二层网络中,掩码存在包含关系即可通信
    • ¥15 端口转发器解析失败不知道电脑设置了啥
    • ¥15 Latex算法流程图行号自定义
    • ¥40 基于51单片机实现球赛计分器功能