Skip to content

Instantly share code, notes, and snippets.

@rpgreen
Last active March 3, 2020 09:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpgreen/5cd3d5c872781335a5d3 to your computer and use it in GitHub Desktop.
Save rpgreen/5cd3d5c872781335a5d3 to your computer and use it in GitHub Desktop.
An API Gateway mapping template to output all HTTP parameters in the JSON body
#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
}
}
@carlnordenfelt
Copy link

I was unable to get this to run but by removing the quotes around $input.json('$') it seems to work:

"body-json" : $input.json('$'),

Before I did that I received an error when posting a json body:

Body:

{
     "hello": "world" 
}

Error:

Could not parse request body into json: Unexpected character (\'h\'

@kfei
Copy link

kfei commented Apr 4, 2016

@carlnordenfelt Thanks! That quotes cost me several hours.....

@JimLynchCodes
Copy link

JimLynchCodes commented May 14, 2018

This mapping template just results in errors for me, even when removing the quotes around $input.json('$'). :(

{"message": "Could not parse request body into json: Unrecognized token 'json': was expecting ('true', 'false' or 'null')\n at [Source: [B@41ebdee4; line: 1, column: 6]"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment