3

I'm trying to have my Api-Gateway map application/x-www-form-urlencoded to json using this solution: How to pass a params from POST to AWS Lambda from Amazon API Gateway

But so far my lambda get successfully triggered only my request.body is always null. If anyone know how to handle that with .net-core c# I'd really appreciate insight.

Here's what my serverless lambda function looks like so far I receive the timestamp but nothing regarding the request.body

public async Task<APIGatewayProxyResponse> Post(APIGatewayProxyRequest request, ILambdaContext context)
    {
        var webHook = new WebHookClient("https://{urlHiddenForObviousReasons}");
        var body = new BodyModel
        {
            Content = $"Trigger @ {DateTime.UtcNow}, {request.Body}"
        };
        await webHook.PostMessage(body);
        var response = new APIGatewayProxyResponse
        {
            StatusCode = (int)HttpStatusCode.OK,
            Body = "Alert received.",
            Headers = new Dictionary<string, string> { { "Content-Type", "text/plain" } }
        };

        return response;
    }

please note that if I use Proxy Integration instead the form values get passed, I want to use mapping so I can have two client using the same api with different post method and have the lambda only parse json. Example of setting that end up passing the form values in this way: key=1&steamid=1&notetitle=ARK-ALARM%3A+06%2F17+%40+16%3A24+on+Paleolithic+Ark+-+The+Island%2C+ALARM+'Base'+IN+'The+Hidden+Lake'+WAS+TRIPPED!&message=...

enter image description here

Lorien
  • 150
  • 2
  • 8
  • 1
    Have you tried to debug the API GW mapping/processing using CW Logs? If you use the 'Test' feature in the API GW Console, you can see a dryrun request with full logging output so you can see the actual raw request being made to Lambda, and also the request body before/after executing the mapping template. – jackko Jun 19 '17 at 17:43

1 Answers1

2

You need to add a Mapping Template. For Example:

{
    "name" : "$input.params('name')",
    "body" : $input.json('$') 
} 

Add this in the Integration Request section of your POST method.AWS API Gateway Integration Request

A really good example is here as well.

Another more complicated example that works for me:

#set($allParams = $input.params())
{
"body-json" : $input.json('$'),
"params" : {
#foreach($type in $allParams.keySet())
    #set($params = $allParams.get($type))
"$type" : {
    #foreach($paramName in $params.keySet())
    "$paramName" : "$util.escapeJavaScript($params.get($paramName))"
        #if($foreach.hasNext),#end
    #end
}
    #if($foreach.hasNext),#end
#end
}
}

I can't comment on the C# code as I'm used to Javascript and Python but it seems fine.

cameck
  • 2,058
  • 20
  • 32
  • well the linked example was doing that mapping dynamically but I'll give a shot with your example that is static. see if it works. – Lorien Jun 18 '17 at 20:20
  • Sorry I took a while to accept your answer. It's what I ended up doing and forgot to come back here. – Lorien Feb 27 '18 at 20:31
  • Hey, no worries, I appreciate you coming back :) – cameck Feb 27 '18 at 20:36