AWS Lambda Function Direct Post Payload Always Null

Viewed 232

I'm trying to use Lambda Functions (C#) with the Function URL for direct access. In postman I'm sending a basic json body that matches the class properties in my input parameter (PostBody). When I execute the POST request, the values are always null tho. Is the input supposed to be something else besides the expected class?

public string FunctionHandler(PostBody input, ILambdaContext context)
{
    LambdaLogger.Log(JsonSerializer.Serialize(input));
    return "Reached Here";
}
2 Answers

Indeed the input must be something else. When your request is handled by the lambda it is mapped to an event object before being passed to the function handler. See the documentation here for more details.

In your case, you can change your input type to APIGatewayHttpApiV2ProxyRequest. Also, you can set the environment variable LAMBDA_NET_SERIALIZER_DEBUG to true in your lambda to see more details in the logs.

I just fought through this. When I test my AWS Lambda using the AWS Console, the incoming first parameter to the FunctionHandler is the expected class based on the JSON payload.

However, when I invoke the Lambda through HTTP POST, the incoming first parameter to the FunctionHandler is an object, which, if you do a .ToString() on it, and pass it back from the Lambda as a response to view it, presents as a large JSON value with outermost keys including "headers", "requestContext", and "body". The JSON data passed in the HTTP POST I found to be subkeys and values inside "body". I found I needed to parse this data (the data in "body") to get the input data I was sending. Once I coded this up, the Lambda worked when invoked through HTTP POST (and it failed when testing it through the AWS Console).

Related