All Downloads are FREE. Search and download functionalities are using the official Maven repository.

.lambadaframework.lambada-maven-plugin.0.0.7.source-code.urlencoded-mapping-template.txt Maven / Gradle / Ivy

The newest version!

## convert HTML POST data or HTTP GET query string to JSON

## get the raw post data from the AWS built-in variable and give it a nicer name
#set($rawAPIData = $input.params().querystring)
#set($rawAPIData = $rawAPIData.toString())
#set($rawAPIDataLength = $rawAPIData.length() - 1)
#set($rawAPIData = $rawAPIData.substring(1, $rawAPIDataLength))
#set($rawAPIData = $rawAPIData.replace(", ", "&"))
#set($rawAPIData = $rawAPIData + "&" + $input.path('$'))

## first we get the number of "&" in the string, this tells us if there is more than one key value pair
#set($countAmpersands = $rawAPIData.length() - $rawAPIData.replace("&", "").length())

## if there are no "&" at all then we have only one key value pair.
## we append an ampersand to the string so that we can tokenise it the same way as multiple kv pairs.
## the "empty" kv pair to the right of the ampersand will be ignored anyway.
#if ($countAmpersands == 0)
 #set($rawPostData = $rawAPIData + "&")
#else
 #set($rawPostData = $rawAPIData)
#end

## now we tokenise using the ampersand(s)
#set($tokenisedAmpersand = $rawPostData.split("&"))

## we set up a variable to hold the valid key value pairs
#set($tokenisedEquals = [])

## now we set up a loop to find the valid key value pairs, which must contain only one "="
#foreach( $kvPair in $tokenisedAmpersand )
 #set($countEquals = $kvPair.length() - $kvPair.replace("=", "").length())
 #if ($countEquals == 1)
  #set($kvTokenised = $kvPair.split("="))
  #if ($kvTokenised[0].length() > 0)
   ## we found a valid key value pair. add it to the list.
   #set($devNull = $tokenisedEquals.add($kvPair))
  #end
 #end
#end

## next we set up our loop inside the output structure "{" and "}"

{
  "package": "",
  "pathtemplate": "$context.resourcePath",
  "method": "$context.httpMethod",
  "requestbody": {
    #foreach( $kvPair in $tokenisedEquals )
      ## finally we output the JSON for this pair and append a comma if this isn't the last pair
      #set($kvTokenised = $kvPair.split("="))
      "$util.urlDecode($kvTokenised[0])" : "$util.urlDecode($kvTokenised[1])"
      #if( $foreach.hasNext ),#end
    #end
  },
      #foreach($elem in $input.params().keySet())
        "$elem": {
            #foreach($innerElem in $input.params().get($elem).keySet())
        "$innerElem": "$util.urlDecode($input.params().get($elem).get($innerElem))"#if($foreach.hasNext),#end
      #end
        }#if($foreach.hasNext),#end
      #end
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy