【问题标题】:API-Gateway Integration Request HTTP Header not mapping query string to headerAPI-Gateway 集成请求 HTTP 标头未将查询字符串映射到标头
【发布时间】:2019-11-26 23:12:03
【问题描述】:

在 Api-Gateway 上,我正在尝试设置从“方法请求”查询字符串到“集成请求”标头到 lambda 的映射,但映射永远不会到达 lambda 函数。

在 'Method Request' > 'URL Query String Parameters' 我设置了名字 'customerIdentification'

然后正如文档所说:doc

转到“集成请求”>“HTTP标头”添加名称“userId”并映射到“method.request.querystring.customerIdentification”

package main

import (
    "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”。

但它总是空的

【问题讨论】:

  • 尝试检查json.Marshal()语句返回的错误
  • 刚才又做了一次,因为你问了,cloudwatch 上没有错误

标签: amazon-web-services go aws-lambda aws-api-gateway


【解决方案1】:

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

val, ok := request.Headers["userId"]
if ok { // the key is present
    fmt.Println(val)
}

但是您确定,键“userId”在标题中吗?通常这些键只会在正文中。如果您想进行交叉检查,请尝试将您的 request.Body 解组为 map[string]string 并从那里检索“userd”。

【讨论】:

  • 问题是为什么 aws 没有将参数传递给 GO lambda。在发布问题之前,我正在编组整个请求,并且该值不存在于任何其他字段中
  • 在handleRequest的参数中尝试将请求类型设为map[string]interface{},例如func handleRequest(ctx context.Context, request map[string]interface{}) (events.APIGatewayProxyResponse, error) { },然后尝试编组整个请求。
  • 正如你所问的,在“方法请求”>“URL 查询字符串参数”上,我将其命名为“customerIdentification”。所以你期待 QueryParam 中的关键,如果是这样 get customerIdentification 就像request.QueryStringParameters["customerIdentification"]
【解决方案2】:

我和你有同样的问题,我认为只有在你使用 http 或 aws 服务代理时才会转发 Http Headers,如本文档所示 https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-method-settings-execution-console.html

对于 HTTP 代理或 AWS 服务代理,将集成请求中定义的路径参数、查询字符串参数或标头参数与 HTTP 的方法请求中对应的路径参数、查询字符串参数或标头参数相关联代理或 AWS 服务代理,请执行以下操作...

【讨论】:

    【解决方案3】:

    如果您使用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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-22
      • 2015-10-21
      • 2020-04-18
      • 2018-04-15
      • 1970-01-01
      • 2013-01-25
      • 2012-06-03
      • 1970-01-01
      相关资源
      最近更新 更多