github.com/mweagle/Sparta@v1.15.0/docs/source/resources/provision/apigateway/inputmapping_formencoded.vtl (about)

     1  ## convert HTML POST data or HTTP GET query string to JSON
     2  ## Ref: https://gist.github.com/ryanray/668022ad2432e38493df
     3  
     4  ## get the raw post data from the AWS built-in variable and give it a nicer name
     5  #if ($context.httpMethod == "POST")
     6   #set($rawAPIData = $input.path('$'))
     7  #elseif ($context.httpMethod == "GET")
     8   #set($rawAPIData = $input.params().querystring)
     9   #set($rawAPIData = $rawAPIData.toString())
    10   #set($rawAPIDataLength = $rawAPIData.length() - 1)
    11   #set($rawAPIData = $rawAPIData.substring(1, $rawAPIDataLength))
    12   #set($rawAPIData = $rawAPIData.replace(", ", "&"))
    13  #else
    14   #set($rawAPIData = "")
    15  #end
    16  
    17  ## first we get the number of "&" in the string, this tells us if there is more than one key value pair
    18  #set($countAmpersands = $rawAPIData.length() - $rawAPIData.replace("&", "").length())
    19  
    20  ## if there are no "&" at all then we have only one key value pair.
    21  ## we append an ampersand to the string so that we can tokenise it the same way as multiple kv pairs.
    22  ## the "empty" kv pair to the right of the ampersand will be ignored anyway.
    23  #if ($countAmpersands == 0)
    24   #set($rawPostData = $rawAPIData + "&")
    25  #end
    26  
    27  ## now we tokenise using the ampersand(s)
    28  #set($tokenisedAmpersand = $rawAPIData.split("&"))
    29  
    30  ## we set up a variable to hold the valid key value pairs
    31  #set($tokenisedEquals = [])
    32  
    33  ## now we set up a loop to find the valid key value pairs, which must contain only one "="
    34  #foreach( $kvPair in $tokenisedAmpersand )
    35   #set($countEquals = $kvPair.length() - $kvPair.replace("=", "").length())
    36   #if ($countEquals == 1)
    37    #set($kvTokenised = $kvPair.split("="))
    38    #if($kvTokenised.size() > 1 && $kvTokenised[1].length() > 0)
    39     ## we found a valid key value pair. add it to the list.
    40     #set($devNull = $tokenisedEquals.add($kvPair))
    41    #end
    42   #end
    43  #end
    44  
    45  #*
    46  Provide an automatic pass through template that transforms all inputs
    47  into the JSON payload sent to a golang function. The default behavior passes the 'body'
    48  key as raw string.
    49  
    50  See
    51    https://forums.aws.amazon.com/thread.jspa?threadID=220274&tstart=0
    52    http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
    53  *#
    54  {
    55    "method": "$context.httpMethod",
    56    "body" : {
    57      #foreach( $kvPair in $tokenisedEquals )
    58        ## finally we output the JSON for this pair and append a comma if this isn't the last pair
    59        #set($kvTokenised = $kvPair.split("="))
    60        "$util.urlDecode($kvTokenised[0])" : #if($kvTokenised[1].length() > 0)"$util.urlDecode($kvTokenised[1])"#{else}""#end#if( $foreach.hasNext ),#end
    61      #end
    62    },
    63    "headers": {
    64      #foreach($param in $input.params().header.keySet())
    65      "$param": "$util.escapeJavaScript($input.params().header.get($param))" #if($foreach.hasNext),#end
    66      #end
    67    },
    68    "queryParams": {
    69      #foreach($param in $input.params().querystring.keySet())
    70      "$param": "$util.escapeJavaScript($input.params().querystring.get($param))" #if($foreach.hasNext),#end
    71  
    72      #end
    73    },
    74    "pathParams": {
    75      #foreach($param in $input.params().path.keySet())
    76      "$param": "$util.escapeJavaScript($input.params().path.get($param))" #if($foreach.hasNext),#end
    77  
    78      #end
    79    },
    80    "context" : {
    81      "apiId" : "$util.escapeJavaScript($context.apiId)",
    82      "method" : "$util.escapeJavaScript($context.httpMethod)",
    83      "requestId" : "$util.escapeJavaScript($context.requestId)",
    84      "resourceId" : "$util.escapeJavaScript($context.resourceId)",
    85      "resourcePath" : "$util.escapeJavaScript($context.resourcePath)",
    86      "stage" : "$util.escapeJavaScript($context.stage)",
    87      "identity" : {
    88        "accountId" : "$util.escapeJavaScript($context.identity.accountId)",
    89        "apiKey" : "$util.escapeJavaScript($context.identity.apiKey)",
    90        "caller" : "$util.escapeJavaScript($context.identity.caller)",
    91        "cognitoAuthenticationProvider" : "$util.escapeJavaScript($context.identity.cognitoAuthenticationProvider)",
    92        "cognitoAuthenticationType" : "$util.escapeJavaScript($context.identity.cognitoAuthenticationType)",
    93        "cognitoIdentityId" : "$util.escapeJavaScript($context.identity.cognitoIdentityId)",
    94        "cognitoIdentityPoolId" : "$util.escapeJavaScript($context.identity.cognitoIdentityPoolId)",
    95        "sourceIp" : "$util.escapeJavaScript($context.identity.sourceIp)",
    96        "user" : "$util.escapeJavaScript($context.identity.user)",
    97        "userAgent" : "$util.escapeJavaScript($context.identity.userAgent)",
    98        "userArn" : "$util.escapeJavaScript($context.identity.userArn)"
    99      }
   100    },
   101     "authorizer": {
   102       #foreach($param in $context.authorizer.keySet())
   103       "$param": "$util.escapeJavaScript($context.authorizer.get($param))" #if($foreach.hasNext),#end
   104       #end
   105     }
   106  }
   107