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.03k stars 198 forks source link

Standard library example bugged or unclear, response always text/plain #116

Open iancullinane opened 2 years ago

iancullinane commented 2 years ago

The example for the standard library seems to always return as text/plain regardless of what I try. I have tried a few implementations, my recent attempt is below. Even when setting the header directly, the response header is always text/plain.

The body is always correct (that is to say it is valid json in the body), it seems to only be the header. This is confusing compared to docs which seem to indicate this is inferred when the response has json. I can use the payload v2 type directly, but that feels cumbersome when this should (?) work.

Additionally when I test the lambda locally or in the lambda console, it works as expected.

func main() {

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        response := map[string]string{"number": "five"}
        w.Header().Set("Content-Type", "application/json") 
        json.NewEncoder(w).Encode(response)
    })

    lambda.Start(httpadapter.New(http.DefaultServeMux).ProxyWithContext)
}

I am deploying this via cdk...

...
    const lambda = new myLambda.DockerImageFunction(this, "ECRFunction", {
      role: myLambda,
      environment: {
        CodeVersionString: this.sha,
      },
      code: lambda.DockerImageCode.fromEcr(repo, {
        tag: this.sha,
      }),
    });

    const myLambdaIntegration = new HttpLambdaIntegration(
      "defaultIntergration",
      myLambdaIntegration
    );

    const api = new apigwv2.HttpApi(this, "my-api", {
      defaultDomainMapping: {
        domainName: dn,
      },
    });

    api.addRoutes({
      path: "/v1/{proxy+}",
      methods: [apigwv2.HttpMethod.ANY],
      integration: myLambdaIntegration,
    });
...