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 réponse
0
Réponse acceptée

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}
répondu il y a un an

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions