awslabs / aws-lambda-go-api-proxy

lambda-go-api-proxy makes it easy to port APIs written with Go frameworks such as Gin (https://gin-gonic.github.io/gin/ ) to AWS Lambda and Amazon API Gateway.
Apache License 2.0
1.04k stars 197 forks source link

GetAPIGatewayContext doesn't work when using `ProxyWithContext` #57

Open eschwartz opened 4 years ago

eschwartz commented 4 years ago

I have code that looks something like:

var (
    muxLambda *gorillamux.GorillaMuxAdapter
)

func main() {
    router := mux.NewRouter()

         // ...some routes are defined here...

    muxLambda = gorillamux.New(router)

    lambda.Start(func(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
        return muxLambda.ProxyWithContext(ctx, req)
    })
}

func MyHandler(w http.ResponseWriter, r *http.Request) {
    reqCtx, err := muxLambda.GetAPIGatewayContext(r)
    // Do stuff with reqCtx
}

The call to muxLambda.GetAPIGatewayContext(r) is returning an error:

"No context header in request"

I'm not super familiar with the inner workings of this repo, but it looks like the GetAPIGatewayContext is expecting a custom "X-GoLambdaProxy-ApiGw-Context" header to be set elsewhere in the code, before my route handler is called. It looks like the Proxy() method results in the header getting set (by calling ProxyEventToHTTPRequest, which calls addToHeader), but the ProxyWithContext method doesn't follow this same code path.

Is this a bug in the ProxyWithContext method, or maybe I'm misunderstanding how this is supposed be used?

sapessi commented 4 years ago

When you use the ProxyWithContext method the Lambda and API Gateway context are added to the http.Request context itself. See the addToContext method.

The GetApiGatewayContext method you are trying to use is there for when you use the Proxy method. In that case the context object is serialized as a header in the request.

zacyang commented 3 years ago

could you update the README this is a bit misleading, the method to extract RequestId listed in the readme does not work with the example provided in the README, it only works with Proxy not ProxyWithContext

kumo-rn5s commented 3 years ago

Does anybody know how to get APIGatewayContext when using ProxyWithContext?

jasonblanchard commented 3 years ago

Bumping this issue - the discrepancy in the docs is really frustrating on top of an already frustrating experience with AWS API Gateway (and that comes from a place of ❤️ ! I really want this to work).

@FirosStuart I think the intent is to use these core functions to get these values from the context in the ProxyWithContext case https://github.com/awslabs/aws-lambda-go-api-proxy/blob/2a487f547914fe7cbf2d09f4a2834827c1080469/core/request.go#L234

That said, I'm still struggling to get it to work. Looks like that ☝️ gets the struct I'd expect, but it's not populated with any values. Still unclear if there's something wrong on my side. I stopped debugging it because I think I just want them in the HTTP request headers for portability (i.e. if I want to test things by sending http requests to a regular gin instance) and compatibility (if I want to back out of API Gateway + Lambda entirely).

rob-mccann commented 2 years ago

FYI I was having similar issues - I could see the data coming in the initial RequestContext on the event but not being able to access it via context.

Using GetAPIGatewayContextFromContext instead of GetAPIGatewayV2ContextFromContext worked for me.