【问题标题】:AWS Lambda PayloadsAWS Lambda 有效负载
【发布时间】:2017-02-04 02:41:07
【问题描述】:

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

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

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 不使用API​​网关可以自定义请求参数吗?
    • 你将如何执行 Lambda 函数?
    • AWS CloudWatch 中的调度
    • 您无法在 CloudWatch 事件中自定义参数。
    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

      作为替代方案,如果 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 参数、正文等。

      【讨论】:

        猜你喜欢
        • 2021-07-24
        • 2018-03-08
        • 2020-09-04
        • 2020-07-13
        • 2019-01-11
        • 2020-03-15
        • 2018-04-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多