登录
首页 >  Golang >  Go问答

Lambda 函数在使用 APIGatewayV2HTTPRequest 时接收到空的标头

来源:stackoverflow

时间:2024-02-20 17:12:25 237浏览 收藏

一分耕耘,一分收获!既然都打开这篇《Lambda 函数在使用 APIGatewayV2HTTPRequest 时接收到空的标头》,就坚持看下去,学下去吧!本文主要会给大家讲到等等知识点,如果大家对本文有好的建议或者看到有不足之处,非常欢迎大家积极提出!在后续文章我会继续更新Golang相关的内容,希望对大家都有所帮助!

问题内容

我在 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 函数

映射模板:没有定义模板时(推荐)

内容类型:application/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 日志显示空 http 请求标头

注意:我之前曾尝试将 lambda 代理集成与 apigatewayproxyrequest 结合使用,效果非常好,但我的应用程序还需要集成响应模板,而在使用代理选项时,该模板不可用。

我哪里可能出错了?


解决方案


不确定您具体要寻找什么;我遇到的问题是,当我将 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
    }
}

理论要掌握,实操不能落!以上关于《Lambda 函数在使用 APIGatewayV2HTTPRequest 时接收到空的标头》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>