API-Gateway Integration Request HTTP Header not mapping query string to header

China☆狼群 提交于 2019-12-11 16:32:54

问题


On Api-Gateway i'm trying so set up mapping from 'Method Request' query string to 'integration request' headers to lambda but the mapping never reach the lambda function.

On 'Method Request' > 'URL Query String Parameters' I set it up the name 'customerIdentification'

Then as the documentation says: doc

went to 'Integration Request' > 'HTTP Headers' add a name 'userId' and mapped to '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)
}

I expect that on the golang lambda function code I can retrieve the 'userId' from 'request.Headers'.

but it always come empty


回答1:


To retrieve a value for any key from map in golang you can simply do like this

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

But are you sure, the key 'userId' is in header? usually these kind of keys will be in body only. If you want to cross check try to unmarshal your request.Body into map[string]string and retrieve "userd" from there.




回答2:


I have the same issue with you, I think the Http Headers will only forwarded if you use http or aws service proxy as shown in this document 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...


来源:https://stackoverflow.com/questions/57087356/api-gateway-integration-request-http-header-not-mapping-query-string-to-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!