【发布时间】:2021-04-25 09:56:12
【问题描述】:
我在 Go 中使用 APIGatewayV2HTTPRequest 从我的 lambda 函数发出如下请求:
"github.com/aws/aws-lambda-go/events"
func (mh *MyHandler) HandleRequest(ctx context.Context, gatewayRequest events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {...}
我的这个 lambda 函数的 API 网关设置如下所示:
集成类型:Lambda 函数
映射模板:当没有定义模板时(推荐)
内容类型:应用程序/json
通用模板:方法请求传递
## This template will pass through all parameters including path, querystring, header, stage variables, and context through to the integration endpoint via the body/payload
#set($allParams = $input.params())
{
"body-json" : $input.json('$'),
"params" : {
#foreach($type in $allParams.keySet())
#set($params = $allParams.get($type))
"$type" : {
#foreach($paramName in $params.keySet())
"$paramName" : "$util.escapeJavaScript($params.get($paramName))"
#if($foreach.hasNext),#end
#end
}
#if($foreach.hasNext),#end
#end
},
"stage-variables" : {
#foreach($key in $stageVariables.keySet())
"$key" : "$util.escapeJavaScript($stageVariables.get($key))"
#if($foreach.hasNext),#end
#end
},
"context" : {
"account-id" : "$context.identity.accountId",
"api-id" : "$context.apiId",
"api-key" : "$context.identity.apiKey",
"authorizer-principal-id" : "$context.authorizer.principalId",
"caller" : "$context.identity.caller",
"cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider",
"cognito-authentication-type" : "$context.identity.cognitoAuthenticationType",
"cognito-identity-id" : "$context.identity.cognitoIdentityId",
"cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId",
"http-method" : "$context.httpMethod",
"stage" : "$context.stage",
"source-ip" : "$context.identity.sourceIp",
"user" : "$context.identity.user",
"user-agent" : "$context.identity.userAgent",
"user-arn" : "$context.identity.userArn",
"request-id" : "$context.requestId",
"resource-id" : "$context.resourceId",
"resource-path" : "$context.resourcePath"
}
}
虽然我在 API 网关日志中看到了 HTTP 请求标头,但我没有看到这些标头被传递给我的 lambda 函数。
Cloudwatch Log showing empty HTTP request headers
注意:我之前曾尝试将 Lambda 代理集成与 APIGatewayProxyRequest 一起使用,它确实可以完美运行,但我的应用程序还需要一个集成响应模板,在使用代理选项时该模板不可用。
我哪里出错了?
【问题讨论】:
标签: amazon-web-services go aws-lambda aws-api-gateway request-headers