AWS API Gateway 映射模板返回空响应或缺少引号

wia*_*erb 6 aws-lambda aws-api-gateway

我已经部署了 api 网关项目,该项目集成了返回 base64 编码响应的 lambda (不是代理)函数。示例响应如下所示(请记住,为了简单起见,我删除了一些亚马逊标头并更改了响应):

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
Run Code Online (Sandbox Code Playgroud)

解码前后的响应如下所示:

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
Run Code Online (Sandbox Code Playgroud)

api网关响应映射模板$input.body用于上述响应。我只想返回body值。当我将映射模板更改为请求$util.base64Decode($input.body)的响应正文GET

{
  "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"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我有兴趣从body键返回值(在本例中为数组)。映射模板,例如$util.base64Decode($input.body).path('$.body')使其返回

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}]
Run Code Online (Sandbox Code Playgroud)

它缺少双引号,并且不被视为有效的 json。有一个类似的问题,但它似乎没有解决我的问题AWS API Gateway Mapping Template JSON

映射模板如

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

返回空响应

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


Run Code Online (Sandbox Code Playgroud)

响应映射模板应该如何返回格式正确的响应?