如何使用方法ANY从Lambo函数获取来自ApiGateway的httpmethod?

问题描述 投票:0回答:2

我正在尝试创建一个API网关,它将采用AWS中的任何方法。调用API后,lambda函数将解析发送的消息,并决定从那里做什么。因此,给定API网关方法:

Type: AWS::ApiGateway::Method
    Properties:
      RestApiId: !Ref myRestApi
      ResourceId: !Ref myResource
      HttpMethod: ANY
      AuthorizationType: NONE
      Integration:
        Type: AWS_PROXY
        IntegrationHttpMethod: POST
        Uri:
          Fn::Join:
          - ''
          - - 'arn:aws:apigateway:'
            - Ref: AWS::Region
            - :lambda:path/2015-04-30/functions/
            - Fn::GetAtt:
              - myLambdaFunction
              - Arn
            - /invocations

并且它将成功调用myLambdaFunction,我如何在节点get中获得HttpMethod实际发送的lambda函数?

例如:

exports.handler = (event, context, callback) => {
    const response = {
        statusCode: 200,
        headers: {
            "x-custom-header" : "This exists for reasons."
        }

    };

// I know that event doesn't actually have any httpmethod, but I'm not sure what it does have, or how to use it.
    switch(event.httpmethod) {
      case "POST":
        console.log("POST!!!");
        create(event, context, callback);
        break;
      case "GET":
        console.log("GET!!!");
        read(event, context, callback);
        break;
      case "PUT":
        console.log("PUT!!!");
        update(event, context, callback);
        break;
    }

上面的lambda,应该能够console.log无论采用哪种方法,但我不知道应该取代event.httpmethod这是我刚刚编造的东西。

node.js amazon-web-services aws-lambda aws-api-gateway
2个回答
1
投票

您正在寻找event.httpMethod(注意CAPITAL M)酒店。

如果您不确定Lambda事件具有哪些数据,则始终可以使用以下方式记录结果

console.log(event);

并且结果将在与Lambda函数关联的CloudWatch日志中可见。

对于API Gateway和Lambda之间的代理集成,您可以在AWS API Gateway developer guide中找到有关这些事件的特定详细信息:

{
    "resource": "Resource path",
    "path": "Path parameter",
    "httpMethod": "Incoming request's method name"
    "headers": {String containing incoming request headers}
    "multiValueHeaders": {List of strings containing incoming request headers}
    "queryStringParameters": {query string parameters }
    "multiValueQueryStringParameters": {List of query string parameters}
    "pathParameters":  {path parameters}
    "stageVariables": {Applicable stage variables}
    "requestContext": {Request context, including authorizer-returned key-value pairs}
    "body": "A JSON string of the request payload."
    "isBase64Encoded": "A boolean flag to indicate if the applicable request payload is Base64-encode"
}

或者在AWS Lambda Developer Guide

{
  "path": "/test/hello",
  "headers": {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
    "Accept-Encoding": "gzip, deflate, lzma, sdch, br",
    "Accept-Language": "en-US,en;q=0.8",
    "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",
    "Host": "wt6mne2s9k.execute-api.us-west-2.amazonaws.com",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48",
    "Via": "1.1 fb7cca60f0ecd82ce07790c9c5eef16c.cloudfront.net (CloudFront)",
    "X-Amz-Cf-Id": "nBsWBOrSHMgnaROZJK1wGCZ9PcRcSpq_oSXZNQwQ10OTZL4cimZo3g==",
    "X-Forwarded-For": "192.168.100.1, 192.168.1.1",
    "X-Forwarded-Port": "443",
    "X-Forwarded-Proto": "https"
  },
  "pathParameters": {
    "proxy": "hello"
  },
  "requestContext": {
    "accountId": "123456789012",
    "resourceId": "us4z18",
    "stage": "test",
    "requestId": "41b45ea3-70b5-11e6-b7bd-69b5aaebc7d9",
    "identity": {
      "cognitoIdentityPoolId": "",
      "accountId": "",
      "cognitoIdentityId": "",
      "caller": "",
      "apiKey": "",
      "sourceIp": "192.168.100.1",
      "cognitoAuthenticationType": "",
      "cognitoAuthenticationProvider": "",
      "userArn": "",
      "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48",
      "user": ""
    },
    "resourcePath": "/{proxy+}",
    "httpMethod": "GET",
    "apiId": "wt6mne2s9k"
  },
  "resource": "/{proxy+}",
  "httpMethod": "GET",
  "queryStringParameters": {
    "name": "me"
  },
  "stageVariables": {
    "stageVarName": "stageVarValue"
  }
}

1
投票

事件变量是一个给你的lambda得到的json请求。

为了使你的代码工作,你需要将以下json传递给lambda

{ 
    httpmethod : "value"
}

其中值为POST,GET或PUT。

如果您转到按钮操作右侧的控制台,则可以使用事件json输入创建测试。

© www.soinside.com 2019 - 2024. All rights reserved.