AWS API Gateway Mapping Template returns empty response or missing quotes

Viewed 492

I've deployed api gateway project that integrates lambda (not proxy) function that return base64 encoded response. Example response looks like (bear in mind that for simplicity I removed some amazon headers and changed response):

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 949
Connection: close
Date: Mon, 14 Dec 2020 15:20:39 GMT
X-Pagination-Offset: 0
X-Pagination-Count: 2
X-Pagination-Limit: 100

eyJib2R5IjogW3sidXVpZCI6ICI1M2EzZTk4My1mYjBkLTQ5MzEtYTc5NC1hYTI4NGEzZDNhYjMiLCAiY3JlYXRlZCI6ICIyMDIwLTExLTI3VDE3OjMwOjQzLjQwMTM3OFoifSwgeyJ1dWlkIjogImQyOTFiNmE0LWQ4NDctNGY5Ni05ZWVmLThlMDMyOGM2NjZhMiIsICJjcmVhdGVkIjogIjIwMjAtMTEtMjdUMTc6MzA6NDMuNDAxMjYzWiJ9XSwgImhlYWRlcnMiOiB7IkNvbnRlbnQtTGVuZ3RoIjogIjIwMyIsICJDb25uZWN0aW9uIjogImtlZXAtYWxpdmUiLCAiQWxsb3ciOiAiR0VULCBIRUFELCBPUFRJT05TIiwgIlgtUGFnaW5hdGlvbi1MaW1pdCI6ICIxMDAiLCAiWC1GcmFtZS1PcHRpb25zIjogIlNBTUVPUklHSU4iLCAiWC1QYWdpbmF0aW9uLUNvdW50IjogIjIiLCAiWC1QYWdpbmF0aW9uLU9mZnNldCI6ICIwIiwgIkNvbnRlbnQtRW5jb2RpbmciOiAiZ3ppcCIsICJWYXJ5IjogIkFjY2VwdC1FbmNvZGluZywgQ29va2llIiwgIkRhdGUiOiAiTW9uLCAxNCBEZWMgMjAyMCAxNToyMDozOSBHTVQiLCAiQ29udGVudC1UeXBlIjogImFwcGxpY2F0aW9uL2pzb24ifSwgImNvZGUiOiAyMDB9

The response before and after decoding looks like:

>>> import base64
>>> import json
>>> from pprint import pprint
>>>
>>> s = "eyJib2R5IjogW3sidXVpZCI6ICI1M2EzZTk4My1mYjBkLTQ5MzEtYTc5NC1hYTI4NGEzZDNhYjMiLCAiY3JlYXRlZCI6ICIyMDIwLTExLTI3VDE3OjMwOjQzLjQwMTM3OFoifSwgeyJ1dWlkIjogImQyOTFiNmE0LWQ4NDctNGY5Ni05ZWVmLThlMDMyOGM2NjZhMiIsICJjcmVhdGVkIjogIjIwMjAtMTEtMjdUMTc6MzA6NDMuNDAxMjYzWiJ9XSwgImhlYWRlcnMiOiB7IkNvbnRlbnQtTGVuZ3RoIjogIjIwMyIsICJDb25uZWN0aW9uIjogImtlZXAtYWxpdmUiLCAiQWxsb3ciOiAiR0VULCBIRUFELCBPUFRJT05TIiwgIlgtUGFnaW5hdGlvbi1MaW1pdCI6ICIxMDAiLCAiWC1GcmFtZS1PcHRpb25zIjogIlNBTUVPUklHSU4iLCAiWC1QYWdpbmF0aW9uLUNvdW50IjogIjIiLCAiWC1QYWdpbmF0aW9uLU9mZnNldCI6ICIwIiwgIkNvbnRlbnQtRW5jb2RpbmciOiAiZ3ppcCIsICJWYXJ5IjogIkFjY2VwdC1FbmNvZGluZywgQ29va2llIiwgIkRhdGUiOiAiTW9uLCAxNCBEZWMgMjAyMCAxNToyMDozOSBHTVQiLCAiQ29udGVudC1UeXBlIjogImFwcGxpY2F0aW9uL2pzb24ifSwgImNvZGUiOiAyMDB9"
>>> result = base64.b64decode(s)
>>> pprint(json.loads(result))
{u'body': [{u'created': u'2020-11-27T17:30:43.401378Z',
            u'uuid': u'53a3e983-fb0d-4931-a794-aa284a3d3ab3'},
           {u'created': u'2020-11-27T17:30:43.401263Z',
            u'uuid': u'd291b6a4-d847-4f96-9eef-8e0328c666a2'}],
 u'code': 200,
 u'headers': {u'Allow': u'GET, HEAD, OPTIONS',
              u'Connection': u'keep-alive',
              u'Content-Encoding': u'gzip',
              u'Content-Length': u'203',
              u'Content-Type': u'application/json',
              u'Date': u'Mon, 14 Dec 2020 15:20:39 GMT',
              u'Vary': u'Accept-Encoding, Cookie',
              u'X-Frame-Options': u'SAMEORIGIN',
              u'X-Pagination-Count': u'2',
              u'X-Pagination-Limit': u'100',
              u'X-Pagination-Offset': u'0'}}

An api gateway response mapping template is $input.body for above response. I would like to return only body value. When I change the mapping template to $util.base64Decode($input.body) a response body for GET request is

{
  "code": 200,
  "headers": {
    "Allow": "GET, HEAD, OPTIONS",
    "Content-Encoding": "gzip",
    "Content-Type": "application/json",
    "Date": "Mon, 14 Dec 2020 15:38:09 GMT",
    "Server": "nginx/1.19.0",
    "Vary": "Accept-Encoding, Cookie",
    "X-Frame-Options": "SAMEORIGIN",
    "X-Pagination-Count": "2",
    "X-Pagination-Limit": "100",
    "X-Pagination-Offset": "0",
    "Content-Length": "203",
    "Connection": "keep-alive"
  },
  "body": [
    {
      "uuid": "53a3e983-fb0d-4931-a794-aa284a3d3ab3",
      "created": "2020-11-27T17:30:43.401378Z"
    },
    {
      "uuid": "d291b6a4-d847-4f96-9eef-8e0328c666a2",
      "created": "2020-11-27T17:30:43.401263Z"
    }
  ]
}

I am interested in returning value (in this case array) from body key. Mapping template like $util.base64Decode($input.body).path('$.body') make it returns

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 277
Connection: close
Date: Mon, 14 Dec 2020 15:42:17 GMT
X-Pagination-Offset: 0
X-Pagination-Count: 2
X-Pagination-Limit: 100


[{uuid=53a3e983-fb0d-4931-a794-aa284a3d3ab3, created=2020-11-27T17:30:43.401378Z}, {uuid=d291b6a4-d847-4f96-9eef-8e0328c666a2, created=2020-11-27T17:30:43.401263Z}]

It is missing double quotes and this is not treated as valid json. There was a similar issue but it does not seem to solve my problem AWS API Gateway Mapping Template JSON.

Mapping template like

  • $util.base64Decode($input.body).json('body')
  • $util.parseJson($util.base64Decode($input.body).path('$.body'))
  • $util.toJson($util.base64Decode($input.body).path('$.body'))

return literally empty response

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 0
Connection: close
Date: Mon, 14 Dec 2020 15:44:28 GMT
x-amzn-RequestId: df62422c-fb35-4616-9f6b-0d646c2aa1e2
X-Pagination-Offset: 0
X-Pagination-Count: 2
X-Pagination-Limit: 100


How should response mapping template look like to return properly formatted response?

0 Answers
Related