我正在尝试在Api-Gateway上设置从“方法请求”查询字符串到“集成请求”标头到lambda的映射,但是映射永远不会到达lambda函数。
在“方法请求”>“ URL查询字符串参数”上,我将其设置为“ 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'。
但是它总是空的
答案 0 :(得分:0)
要从golang的地图中检索任何键的值,您可以像这样
val, ok := request.Headers["userId"]
if ok { // the key is present
fmt.Println(val)
}
但是您确定键'userId'在标题中吗?通常,这些键仅在体内。如果您想进行交叉检查,请尝试 解组您的请求。将正文放入map [string] string 并从中检索“ userd”。
答案 1 :(得分:0)
与您有同样的问题,我认为仅在您使用http或aws服务代理(如本文档所示)时,才会转发Http标头 https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-method-settings-execution-console.html
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...
答案 2 :(得分:0)