4

How can i pass a JSON object like {val: 1} to my Lambda function as a query parameter?
Following Standardized way to serialize JSON to query string? i URL-encoded my JSON object and requested the following: mysite.com/path?json=%7B%22val%22%3A%201%7D

As requestTemplates i tried the following two options

  1. "json": "$input.params().querystring.json"
  2. "json": "$util.parseJson($input.params().querystring.json)"

But i got this error message:

{"message": "Could not parse request body into json: Unexpected character (\'v\' (code 118)): was expecting comma to separate OBJECT entries\n at [Source: [B@37a2970e; line: 1, column: 47]"}

If i do not encode the query string so: mysite.com/path?json={"val":1} i get a 400 error

Community
  • 1
  • 1
Rentrop
  • 20,979
  • 10
  • 72
  • 100

1 Answers1

3
  1. Your mapping template is producing no valid JSON, you have to wrap the key/value pair in curly braces
  2. I guess you do not want to wrap the value in quotes, it will be a string and no object otherwise
  3. You can use $util.urlDecode to decode URL encoded strings

Your mapping template should look like this:

{"json": $util.urlDecode($input.params().querystring.json)}

For mysite.com/path?json=%7B%22val%22%3A%201%7D this mapping template will result in the following JSON:

{
  "json": {
    "val": 1
  }
}

If you want the querystring JSON to be passed on the root level to your Lambda function use this as a mapping template:

$util.urlDecode($input.params().querystring.json)
birnbaum
  • 4,718
  • 28
  • 37