为了账号安全,请及时绑定邮箱和手机立即绑定

API 网关集成请求 HTTP 标头未将查询字符串映射到标头

API 网关集成请求 HTTP 标头未将查询字符串映射到标头

Go
手掌心 2023-07-04 16:58:01
在 Api-Gateway 上,我尝试设置从“方法请求”查询字符串到“集成请求”标头到 lambda 的映射,但映射永远不会到达 lambda 函数。在“方法请求”>“URL 查询字符串参数”上,我将其设置为名称“customerIdentification”然后正如文档所说:doc转到“集成请求”>“HTTP 标头”添加名称“userId”并映射到“method.request.querystring.customerIdentification”package mainimport (    "context"    "encoding/json"    "fmt"    "github.com/aws/aws-lambda-go/events"    "github.com/aws/aws-lambda-go/lambda")func handleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {    fmt.Printf("Processing request data for request %s.\n", request.RequestContext.RequestID)    fmt.Printf("Body size = %d.\n", len(request.Body))    fmt.Println("Headers:")    for key, value := range request.Headers {        fmt.Printf("    %s: %s\n", key, value)    }    xxx, err := json.Marshal(request.Headers)    if err != nil {        fmt.Println("*** err *** err *** err *** err *** err ")        fmt.Println(err)        fmt.Println("*** err *** err *** err *** err *** err ")    }    return events.APIGatewayProxyResponse{Body: string(xxx), StatusCode: 200}, nil}func main() {    lambda.Start(handleRequest)}我希望在 golang lambda 函数代码中我可以从“request.Headers”中检索“userId”。但它总是空的
查看完整描述

3 回答

?
守候你守候我

TA贡献1802条经验 获得超10个赞

要从 golang 中的映射中检索任何键的值,您可以简单地这样做


val, ok := request.Headers["userId"]

if ok { // the key is present

    fmt.Println(val)

}

但是您确定键“userId”位于标头中吗?通常这些类型的钥匙仅位于机身中。如果您想交叉检查,请尝试将您的 request.Body 解组到 map[string]string并从那里检索“userd”。


查看完整回答
反对 回复 2023-07-04
?
波斯汪

TA贡献1811条经验 获得超4个赞

我和你有同样的问题,我认为只有当你使用 http 或 aws 服务代理时才会转发 Http 标头,

For an HTTP proxy or an AWS service proxy, to associate a path parameter, a query string parameter, or a header parameter defined in the integration request with a corresponding path parameter, query string parameter, or header parameter in the method request of the HTTP proxy or AWS service proxy, do the following...



查看完整回答
反对 回复 2023-07-04
?
繁华开满天机

TA贡献1816条经验 获得超4个赞

如果您正在使用,Rest API请确保将集成类型设置为AWS_PROXY。它允许您处理事件并让您完全控制响应。


使用CLI:


$ aws apigateway put-integration --rest-api-id {rest-api-id} \

--resource-id {resource-id} --http-method ANY --type AWS_PROXY \

--integration-http-method {http-method} \

--uri {uri}

使用Console:

//img1.sycdn.imooc.com//64a3df4d0001d60d13690669.jpg

查看完整回答
反对 回复 2023-07-04
  • 3 回答
  • 0 关注
  • 103 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信