Using API Gateway Request Templates in Lambda Integration for parsing JSON results in empty string

0

Is it possible to pass parsed JSON to Lambda? Or does API Gateway support only passing strings to integrations where the string must be parsed as JSON?

The goal is to map a query string parameter myJson (as in GET /api/endpoint?myJson=%7B%5C%22x%5C%22%3A123%7D) to a parsed JSON object, such that it is accessible in the Lambda handler without the need for (using JavaScript) JSON.parse(event.queryStringParameters.myJson).

However, using the request template below:

{
    "myParsedJson": "$util.parseJson($input.params('myJson'))"
}

or

{
    "myParsedJson": "$util.parseJson($util.urlDecode($input.params('myJson')))"
}

gives an event in Lambda that is:

{
    myParsedJson: ''
}

What I expected is:

{
    myParsedJson: { x: 123 }
}

Any pointers?

1 Resposta
0
Resposta aceita

Hello,

You may just try this $util.urlDecode($input.params('myJson')) and provide the query string value GET /api/endpoint?myJson=%7B%22x%22%3A123%7D and it should work as you expect. you need not to worry about parsing as you are providing json string value of json with encode, hence by decoding you will get json string back. if you like to use stringify version containing "/" then you have to replace "/" with nothing to get json string. hope it helps. thanks

Code snippet:

"myParsedJson" : $util.urlDecode($input.params('myJson'))

output:

myParsedJson : {"x":123}
respondido há um ano

Você não está conectado. Fazer login para postar uma resposta.

Uma boa resposta responde claramente à pergunta, dá feedback construtivo e incentiva o crescimento profissional de quem perguntou.

Diretrizes para responder a perguntas