AWS Lambda Payloads

时间:2015-08-17 09:27:58

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

我在AWS Lambda文档上搜索但找不到问题的答案。

有没有办法可以从Lambda函数访问整个请求体(用node.js编写)?

event参数似乎只包含已解析的JSON属性。

3 个答案:

答案 0 :(得分:2)

您的请求正文需要使用XML或JSON才能在Lambda函数中访问它。您需要指定在Integration Request信息中心的API Gateway部分中处理/映射和传递的方式。

答案 1 :(得分:1)

一旦您在身体映射模板中公开请求正文,您就可以访问AWS Lambda中的请求正文。

  1. 在API网关控制台中打开您的方法
  2. 打开Integration Request
  3. Integration Request中,打开Body Mapping Templates面板
  4. 添加Content Type application/json
  5. 点击新创建的application/json商品
  6. 添加以下模板:
  7.  {
       "body" : $input.json('$')
     }
    

    之后,您可以在Node.js Lambda函数中以event.body的形式访问请求正文。

答案 2 :(得分:0)

作为替代方案,如果它更适合您的用例,您可以考虑将lambda设置为简单代理。我最近发现有更多人使用这种技术。

http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html

以下请求:

POST /testStage/hello/world?name=me HTTP/1.1
Host: gy415nuibc.execute-api.us-east-1.amazonaws.com
Content-Type: application/json
headerName: headerValue

{
    "a": 1
}

最终将以下事件数据发送到您的AWS Lambda函数:

{
  "message": "Hello me!",
  "input": {
    "resource": "/{proxy+}",
    "path": "/hello/world",
    "httpMethod": "POST",
    "headers": {
      "Accept": "*/*",
      "Accept-Encoding": "gzip, deflate",
      "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",
      "headerName": "headerValue",
      "Host": "gy415nuibc.execute-api.us-east-1.amazonaws.com",
      "Postman-Token": "9f583ef0-ed83-4a38-aef3-eb9ce3f7a57f",
      "User-Agent": "PostmanRuntime/2.4.5",
      "Via": "1.1 d98420743a69852491bbdea73f7680bd.cloudfront.net (CloudFront)",
      "X-Amz-Cf-Id": "pn-PWIJc6thYnZm5P0NMgOUglL1DYtl0gdeJky8tqsg8iS_sgsKD1A==",
      "X-Forwarded-For": "54.240.196.186, 54.182.214.83",
      "X-Forwarded-Port": "443",
      "X-Forwarded-Proto": "https"
    },
    "queryStringParameters": {
      "name": "me"
    },
    "pathParameters": {
      "proxy": "hello/world"
    },
    "stageVariables": {
      "stageVariableName": "stageVariableValue"
    },
    "requestContext": {
      "accountId": "12345678912",
      "resourceId": "roq9wj",
      "stage": "testStage",
      "requestId": "deef4878-7910-11e6-8f14-25afc3e9ae33",
      "identity": {
        "cognitoIdentityPoolId": null,
        "accountId": null,
        "cognitoIdentityId": null,
        "caller": null,
        "apiKey": null,
        "sourceIp": "192.168.196.186",
        "cognitoAuthenticationType": null,
        "cognitoAuthenticationProvider": null,
        "userArn": null,
        "userAgent": "PostmanRuntime/2.4.5",
        "user": null
      },
      "resourcePath": "/{proxy+}",
      "httpMethod": "POST",
      "apiId": "gy415nuibc"
    },
    "body": "{\r\n\t\"a\": 1\r\n}",
    "isBase64Encoded": false
  }
}

现在,您可以访问每个请求中的所有标题,网址参数,正文等。