gpt4 book ai didi

node.js - 如何让 Lambda 函数使用 ANY 方法从 ApiGateway 获取 httpmethod?

转载 作者:太空宇宙 更新时间:2023-11-03 22:57:53 24 4
gpt4 key购买 nike

我正在尝试创建一个 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,那么我如何让 Node 中的 lambda 函数获取实际发送的 HttpMethod

例如:

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

最佳答案

您正在查找 event.httpMethod(注意大写 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"
}
}

关于node.js - 如何让 Lambda 函数使用 ANY 方法从 ApiGateway 获取 httpmethod?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55225768/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com