|
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 "}" |
35 | 1 | { |
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 |
41 | 15 | } |
0 commit comments