Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

carlnordenfelt commented Mar 24, 2016

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

This comment has been minimized.

kfei commented Apr 4, 2016

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

@JimTheMan

This comment has been minimized.

JimTheMan 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