0
votes

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.\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)
}

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

1
I guess you have mapped that lambda to an GET http call in your APIGateway and they never have a body. Try printing request.Path for example.Jonny Rimek
@JonnyRimek, It's a POST call in my API Gateway. In Cloudwatch, the logs say: Endpoint request body after transformations: {"my": "body data"}. Then they say: Sending request to lambda.us-east-1.amazonaws.com/my-lamba-path, so I'm pretty sure the request body is being sent to the Lambda?Ben Freed
Kind of frustrating to see my question downvoted with no comment. If there an issue with the question, please let me know in the comments.Ben Freed
I don't see an obvious error. gitlab.com/jrimek/tnp/api/blob/master/handler/create-category/… this is a lambda that receives a POST request and writes an item to DynamoDB, maybe you see something that can help youJonny Rimek

1 Answers

0
votes

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.