Skip to content
This repository was archived by the owner on Jun 26, 2024. It is now read-only.

Commit 18424fa

Browse files
committed
Updated APIGW mapping template
1 parent 751ebf9 commit 18424fa

2 files changed

Lines changed: 14 additions & 40 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Step 1 – Create an AWS CloudFormation stack with the [template](https://s3.ama
3333

3434
Step 2 – Visit the [API Gateway dashboard](https://console.aws.amazon.com/apigateway/home) in your AWS account and create a new resource with a `/vote` endpoint. Assign a POST method that has the `Integration Request` type of "Lambda Function," and point to the Lambda function created by the AWS CloudFormation script that receives votes from your third-party voting service (in this example, Twilio).
3535

36-
Under `Mapping Templates`, set the "Content-Type" to `application/x-www-form-urlencoded`, and add [this mapping template](apigateway-mappingtemplate.txt).
36+
Under `Body Mapping Templates`, set the "Content-Type" to `application/x-www-form-urlencoded`, and add [this mapping template](apigateway-mappingtemplate.txt).
3737

3838
Step 3 – Visit the [Amazon Cognito dashboard](https://console.aws.amazon.com/cognito/home) and create a new identity pool that allows access to unauthenticated identities. Modify the policy document to allow read access to the aggregates DynamoDB table created by the AWS CloudFormation script above. This allows unauthenticated users to retrieve data from the vote aggregation table in DynamoDB. Amazon Cognito will provide sample code for the JavaScript platform. Note the value for identity pool ID; you'll need it in step 4.
3939

apigateway-mappingtemplate.txt

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,15 @@
1-
## convert HTML FORM POST data to JSON for insertion directly into a Lambda function
2-
3-
## get the raw post data from the AWS built-in variable and give it a nicer name
4-
#set($rawPostData = $input.path('$'))
5-
6-
## first we get the number of "&" in the string, this tells us if there is more than one key value pair
7-
#set($countAmpersands = $rawPostData.length() - $rawPostData.replace("&", "").length())
8-
9-
## if there are no "&" at all then we have only one key value pair.
10-
## we append an ampersand to the string so that we can tokenise it the same way as multiple kv pairs.
11-
## the "empty" kv pair to the right of the ampersand will be ignored anyway.
12-
#if ($countAmpersands == 0)
13-
#set($rawPostData = $rawPostData + "&")
14-
#end
15-
16-
## now we tokenise using the ampersand(s)
17-
#set($tokenisedAmpersand = $rawPostData.split("&"))
18-
19-
## we set up a variable to hold the valid key value pairs
20-
#set($tokenisedEquals = [])
21-
22-
## now we set up a loop to find the valid key value pairs, which must contain only one "="
23-
#foreach( $kvPair in $tokenisedAmpersand )
24-
#set($countEquals = $kvPair.length() - $kvPair.replace("=", "").length())
25-
#if ($countEquals == 1)
26-
#set($kvTokenised = $kvPair.split("="))
27-
#if ($kvTokenised[0].length() > 0)
28-
## we found a valid key value pair. add it to the list.
29-
#set($devNull = $tokenisedEquals.add($kvPair))
30-
#end
31-
#end
32-
#end
33-
34-
## next we set up our loop inside the output structure "{" and "}"
351
{
36-
#foreach( $kvPair in $tokenisedEquals )
37-
## finally we output the JSON for this pair and append a comma if this isn't the last pair
38-
#set($kvTokenised = $kvPair.split("="))
39-
"$kvTokenised[0]" : #if($kvTokenised[1].length() > 0)"$kvTokenised[1]"#{else}""#end#if( $foreach.hasNext ),#end
40-
#end
2+
#foreach( $token in $input.path('$').split('&') )
3+
#set( $keyVal = $token.split('=') )
4+
#set( $keyValSize = $keyVal.size() )
5+
#if( $keyValSize >= 1 )
6+
#set( $key = $util.urlDecode($keyVal[0]) )
7+
#if( $keyValSize >= 2 )
8+
#set( $val = $util.urlDecode($keyVal[1]) )
9+
#else
10+
#set( $val = '' )
11+
#end
12+
"$key": "$val"#if($foreach.hasNext),#end
13+
#end
14+
#end
4115
}

0 commit comments

Comments
 (0)