lifadev / archive_aws-lambda-go-event

Type definitions for AWS Lambda event sources.

Home Page:https://github.com/eawsy/aws-lambda-go-event

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to access queryparameterstrings in aws lambda proxy events

sateeshyandapalli-saven opened this issue · comments

Hi, i am trying to create a lambda function using aws apigateway proxy method. even though i pass query parameters, it shows null
image

Hi @sateeshyandapalli,
Can you please provide a minimal example of how you've configured your API Gateway (a cloudformation template or so). And please take a look at https://github.com/eawsy/aws-lambda-go-net
(along with its deployment template) which uses the API Gateway proxy event successfully.

The example code doesn't work for me (would cause 502 gateway error).

But the code following works for me, you can have a try. @sateeshyandapalli

package main

import (
	"encoding/json"

	"github.com/eawsy/aws-lambda-go-core/service/lambda/runtime"
	"github.com/eawsy/aws-lambda-go-event/service/lambda/runtime/event/apigatewayproxyevt"
)

func Handle(evt *apigatewayproxyevt.Event, ctx *runtime.Context) (interface{}, error) {
	buf, err := json.Marshal(evt)
	if err != nil {
		return nil, err
	}

	return map[string]interface{}{
		"statusCode": 200,
		"body":       string(buf),
	}, nil
}

Hi @hilyjiang,
Can you please provide more details about what is not working. Do you talk about the go-net examples? Which one?

@fsenart

I try to create a API with Lambda Proxy Itergration, using the code from:
https://github.com/eawsy/aws-lambda-go-event/tree/master/service/lambda/runtime/event/apigatewayproxyevt

package main

import (
	"log"

	"github.com/eawsy/aws-lambda-go-event/service/lambda/runtime/event/apigatewayproxyevt"
	"github.com/eawsy/aws-lambda-go-core/service/lambda/runtime"
)

func Handle(evt *apigatewayproxyevt.Event, ctx *runtime.Context) (interface{}, error) {
	log.Println(evt)
	return nil, nil
}

But the output format of Handle didn't match the format mentioned in:
https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html

So I modified it to the code in the above post, and it works.

@hilyjiang you're right in the fact that the provided example won't work behind api gateway proxy mode. But notice that all provided examples (other events included) are only here to show the "shape" of the incoming event and mostly work when using the AWS Lambda console test events. Otherwise we have to provide for each event the exact way it will integrate with each service in different contexts, etc.
And that's why complex events like API Gateway Proxy have their own projects like https://github.com/eawsy/aws-lambda-go-net to provide real usability.

BTW, you confirm that when using your snippet you have access to query strings? So I can close this issue for now as I haven't feedback from @sateeshyandapalli.

Yes, the query strings work normal.

Request: /user?email=hilyjiang@gmail.com
Response Body:

{
  "HTTPMethod": "GET",
  "Headers": null,
  "Resource": "/user",
  "PathParameters": null,
  "Path": "/user",
  "QueryStringParameters": {
    "email": "hilyjiang@gmail.com"
  },
  "Body": "",
  "IsBase64Encoded": false,
  "StageVariables": null,
  "RequestContext": {
    "APIID": "za4usr3af8",
    "ResourceID": "iy0ic1",
    "RequestID": "test-invoke-request",
    "HTTPMethod": "GET",
    "ResourcePath": "/user",
    "AccountID": "072791703432",
    "Stage": "test-invoke-stage",
    "Identity": {
    ...hide...
    },
    "Authorizer": null
  }
}