AWS API网关主体映射模板中的原始主体负载

时间:2016-06-08 21:41:22

标签: json amazon-web-services aws-lambda aws-api-gateway

出于某种原因,我很难从event中获取原始身体。它将$input.body记录为application/json内容类型的json。 The docs say that that should contain the raw payload.

这是我的集成请求主体映射模板:

{
  "body" : $input.json('$'),
  "rawBody": $input.body,
  "headers": {
    #foreach($header in $input.params().header.keySet())
    "$header": "$util.escapeJavaScript($input.params().header.get($header))" #if($foreach.hasNext),#end

    #end
  },
  "method": "$context.httpMethod",
  "params": {
    #foreach($param in $input.params().path.keySet())
    "$param": "$util.escapeJavaScript($input.params().path.get($param))" #if($foreach.hasNext),#end

    #end
  },
  "query": {
    #foreach($queryParam in $input.params().querystring.keySet())
    "$queryParam": "$util.escapeJavaScript($input.params().querystring.get($queryParam))" #if($foreach.hasNext),#end

    #end
  }  
}

这是有效负载示例:

{
  "event": {
    "body": {
      "hello": "meow"
    },
    "rawBody": {
      "hello": "meow"
    },
    "headers": {
      "Accept": "*/*",
      "Accept-Encoding": "gzip, deflate",
      "Accept-Language": "en-US",
      "Cache-Control": "no-cache",
      "CloudFront-Forwarded-Proto": "https",
      "CloudFront-Is-Desktop-Viewer": "true",
      "CloudFront-Is-Mobile-Viewer": "false",
      "CloudFront-Is-SmartTV-Viewer": "false",
      "CloudFront-Is-Tablet-Viewer": "false",
      "CloudFront-Viewer-Country": "US",
      "Content-Type": "application/json",
      "Host": "7nuy7lymef.execute-api.us-east-1.amazonaws.com",
      "Origin": "file://",
      "Postman-Token": "0ce7c6f4-3864-c9b4-f2db-739737b2ba49",
      "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Postman/4.2.2 Chrome/47.0.2526.73 Electron/0.36.2 Safari/537.36",
      "Via": "1.1 1eea0bca59557555878da4d9775c509f.cloudfront.net (CloudFront)",
      "X-Amz-Cf-Id": "SDjaGcuJ5eVkOMMCn6M3vGaVicA1fuA7h0bUYE4ARlKupO60eeYNFA==",
      "X-Forwarded-For": "206.71.230.14, 205.251.250.135",
      "X-Forwarded-Port": "443",
      "X-Forwarded-Proto": "https",
      "x_example_header": "my awesome header"
    },
    "method": "POST",
    "params": {},
    "query": {
      "example_param": "myawesomeparam"
    }
  },
  "context": {
    "callbackWaitsForEmptyEventLoop": false,
    "logGroupName": "/aws/lambda/reggi-log-post",
    "logStreamName": "2016/06/08/[$LATEST]aad04e0e46614c288ac8ca43d0a95076",
    "functionName": "reggi-log-post",
    "memoryLimitInMB": "128",
    "functionVersion": "$LATEST",
    "invokeid": "6e4e1e13-2dc1-11e6-a1f7-4dad3a8eb122",
    "awsRequestId": "6e4e1e13-2dc1-11e6-a1f7-4dad3a8eb122",
    "invokedFunctionArn": "arn:aws:lambda:us-east-1:562508364089:function:reggi-log-post"
  }
}
  • 有没有办法从此请求访问原始主体?
  • 有没有办法更改内容类型以接受所有类型?

2 个答案:

答案 0 :(得分:3)

以下博客文章详细解释了如何解决此问题。 https://nicholasjackson.io/2016/12/13/using-graphql-with-aws-lambda/

它专门在GraphQL的上下文中编写,但它适用于任何内容类型。简而言之:

  • 转到二进制支持部分。为您选择的媒体类型启用二进制支持并保存。
  • 返回参考资料部分中的方法并打开Integration Request。添加/编辑所选内容类型的正文映射模板,并输入以下内容:

    "rawBody": "$util.escapeJavaScript($util.base64Decode($input.body))"

  • 保存并重新部署API。

添加二进制支持将请求编码为base64字符串。身体映射模板对其进行解码。

答案 1 :(得分:1)

$input.body包含原始有效负载。你需要像"rawBody": "$input.body"那样在它周围加上引号。否则,正文将被解释为json文档的一部分。