首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Lambda函数中无法获取API网关`RequestId`

在Lambda函数中无法获取API网关`RequestId`
EN

Stack Overflow用户
提问于 2018-06-30 00:28:07
回答 2查看 2.8K关注 0票数 3

我发送了一篇帖子,并从amazon API Gateway获得了如下响应:

代码语言:javascript
复制
{"MD5OfMessageBody": "bc709a65868c21309969a07cfeeac12f", "MD5OfMessageAttributes": "8a66ae91e9109cb75717520c287736eb", "MessageId": "b70a5453-5f30-4914-8006-b6cf7fa4ff93", "ResponseMetadata": {"RequestId": "1a38fbb5-859a-53f9-9551-a805d864768c", "HTTPStatusCode": 200, "HTTPHeaders": {"server": "Server", "date": "Fri, 29 Jun 2018 16:18:06 GMT", "content-type": "text/xml", "content-length": "459", "connection": "keep-alive", "x-amzn-requestid": "1a38fbb5-859a-53f9-9551-a805d864768c"}, "RetryAttempts": 0}}

当我查看传入lambda:def lambda_handler(event, context):中的处理程序函数的事件对象时

代码语言:javascript
复制
{'resource': '/python_job_to_sqs', 'path': '/python_job_to_sqs', 'httpMethod': 'POST', 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', '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', 'Host': '5p1xcqn2y5.execute-api.us-east-1.amazonaws.com', 'User-Agent': 'python-requests/2.14.2', 'Via': '1.1 c7db0c4b178dd73a64add79be10805c2.cloudfront.net (CloudFront)', 'X-Amz-Cf-Id': 'QkdNKg7VrrB6tJ0wynZwYvCxSYpLMTGkm9CK6CsYhw-jLDqTI0-x6g==', 'X-Amzn-Trace-Id': 'Root=1-5b365bbd-9dd143fc6325d468485f44a0', 'X-Forwarded-For': '38.77.16.218, 204.246.180.73', 'X-Forwarded-Port': '443', 'X-Forwarded-Proto': 'https'}, 'queryStringParameters': None, 'pathParameters': None, 'stageVariables': None, 'requestContext': {'resourceId': 'i42qmv', 'resourcePath': '/python_job_to_sqs', 'httpMethod': 'POST', 'extendedRequestId': 'JQNFqFZjoAMFf8g=', 'requestTime': '29/Jun/2018:16:18:05 +0000', 'path': '/production/python_job_to_sqs', 'accountId': '130491730454', 'protocol': 'HTTP/1.1', 'stage': 'production', 'requestTimeEpoch': 1530289085866, 'requestId': '012c013f-7bb8-11e8-bed9-1ff9514513cd', 'identity': {'cognitoIdentityPoolId': None, 'accountId': None, 'cognitoIdentityId': None, 'caller': None, 'sourceIp': '38.77.16.218', 'accessKey': None, 'cognitoAuthenticationType': None, 'cognitoAuthenticationProvider': None, 'userArn': None, 'userAgent': 'python-requests/2.14.2', 'user': None}, 'apiId': '5p1xcqn2y5'}, 'body': '{"input": [{"value": "test.co", "fieldName": "tld"}]}, 'isBase64Encoded': False}

似乎在其他框架中,requestContext['requestId'] (这里是012c013f-7bb8-11e8-bed9-1ff9514513cd )应该是从API Gateway传入的请求ID。但是012c013f-7bb8-11e8-bed9-1ff9514513cd != 1a38fbb5-859a-53f9-9551-a805d864768c。他们将其替换为lambda函数ID,这是他们框架中的一个bug吗?

EN

回答 2

Stack Overflow用户

发布于 2021-07-09 15:04:55

您可以通过使用‘requestId’轻松获取接口网关的事件。

代码语言:javascript
复制
 def lambda_handler(event, context):
   request = event["requestContext"]
   request_id = request["requestId"]
票数 1
EN

Stack Overflow用户

发布于 2018-07-02 01:29:21

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51105990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档