Lorien
Lorien

Reputation: 160

How to pass a parameter from POST to AWS Lambda from Amazon API Gateway

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

Upvotes: 3

Views: 4937

Answers (1)

cameck
cameck

Reputation: 2098

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.

Upvotes: 2

Related Questions