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개 답변
0
수락된 답변

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}
답변함 일 년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠