6

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

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

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

4

3 回答 3

2

您的请求正文需要采用 XML 或 JSON 格式,以便能够在您的Lambda函数中访问它。Integration Request您需要在仪表板部分指定如何处理/映射和传递它API Gateway

于 2015-08-17T21:23:55.250 回答
1

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

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

之后,您可以像event.body在 Node.js Lambda 函数中一样访问请求正文。

于 2017-02-03T18:13:35.303 回答
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
  }
}

现在您可以访问每个请求中的所有标头、url 参数、正文等。

于 2017-01-02T20:34:32.660 回答