【问题标题】:Lambda function receiving empty headers when using APIGatewayV2HTTPRequest使用 APIGatewayV2HTTPRequest 时接收空标头的 Lambda 函数
【发布时间】: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


    【解决方案1】:

    不确定您具体要寻找什么;我遇到的问题是,当我将 HTTP API 连接到 Lambda 函数时,如何获取调用方法并不明显 - 经过一些挖掘和试验,这很有效:

    func Handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (events.APIGatewayProxyResponse, error) {
    
        log.Info("method = " + req.RequestContext.HTTP.Method)
    
        if req.RequestContext.HTTP.Method == "GET" {
            return events.APIGatewayProxyResponse{
                StatusCode: http.StatusForbidden,
                Body:       `{"message": "HTTP GET method not supported on this endpoint."}`,
            }, nil
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-11-12
      • 2021-05-01
      • 1970-01-01
      • 2017-07-25
      • 2020-09-12
      • 2017-07-24
      • 2020-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多