Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

502 Bad Gateway error because Headers field is nil #79

Open
kdeloach opened this issue Oct 8, 2020 · 7 comments
Open

502 Bad Gateway error because Headers field is nil #79

kdeloach opened this issue Oct 8, 2020 · 7 comments

Comments

@kdeloach
Copy link

kdeloach commented Oct 8, 2020

I get 502 Bad Gateway response from AWS LB using the exact source code from the README with the latest release v0.8.1.

This is due to a recent change in #73 which results in Headers being set to nil on the proxy response. You can work around this by adding Headers to the proxied response.

Lambda output and HTTP response using the code from the README:

{
  "statusCode": 200,
  "headers": null,
  "multiValueHeaders": {
    "Content-Type": [
      "application/json; charset=utf-8"
    ]
  },
  "body": "{\"message\":\"pong\"}"
}
$ curl http://<removed>.elb.amazonaws.com/ping
<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
</body>
</html>

My workaround:

func Handler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
	// If no name is provided in the HTTP request body, throw an error
	resp, err := ginLambda.ProxyWithContext(ctx, req)
	headers := make(map[string]string)
	headers["Content-Type"] = "application/json"
	resp.Headers = headers
	return resp, err
}

Lambda output and HTTP response with this workaround:

{
  "statusCode": 200,
  "headers": {
    "Content-Type": "application/json"
  },
  "multiValueHeaders": {
    "Content-Type": [
      "application/json; charset=utf-8"
    ]
  },
  "body": "{\"message\":\"pong\"}"
}
$ curl -i http://<removed>.elb.amazonaws.com/ping
HTTP/1.1 200 OK
Server: awselb/2.0
Date: Thu, 08 Oct 2020 18:59:03 GMT
Content-Type: application/json
Content-Length: 18
Connection: keep-alive

{"message":"pong"}
@NotSoSuper
Copy link

Can confirm this issue with Application Load Balancers too, reverting #73 results in a properly working HTTP response.

Only noticed this issue after updating from v0.8.0 to v0.12.0

@tgilino
Copy link

tgilino commented Apr 6, 2022

Ran into this issue myself with only Lambda functions (not using HTTP API or ALB), and additionally HTTP API Payload Format 2.0 has removed multi-value headers and just uses headers. I don't think there should be a non-pointer field that is not initialized.

Is there a plan to fix?

@mcku
Copy link

mcku commented Nov 29, 2022

I think I've faced this issue and realized that the Headers field always nil. (https://github.com/awslabs/aws-lambda-go-api-proxy/blob/master/core/response.go#L108)

In my case, API Gateway failed with internal error until headers was populated with content type at least.

@louism517
Copy link

Are there any plans to fix this?
Its a bit annoying to have to craft some dummy headers just to make it work.

@kubamarchwicki
Copy link

The fix is in the AWS target group. By default, ALB requires a content-type header and by default it ignores multi-value headers (https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#enable-multi-value-headers).

Even though the aws-lambda-go-api-proxy sets the default headers, they are ignored by the ALB. Had the same issues and setting the multi-value header flag to true resolved issues for me

@iguoyr
Copy link

iguoyr commented May 10, 2024

The fix is in the AWS target group. By default, ALB requires a content-type header and by default it ignores multi-value headers (https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#enable-multi-value-headers).

Even though the aws-lambda-go-api-proxy sets the default headers, they are ignored by the ALB. Had the same issues and setting the multi-value header flag to true resolved issues for me

Works for me! tks!

@xosblo91
Copy link

xosblo91 commented Feb 7, 2025

The fix is in the AWS target group. By default, ALB requires a content-type header and by default it ignores multi-value headers (https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#enable-multi-value-headers).

Even though the aws-lambda-go-api-proxy sets the default headers, they are ignored by the ALB. Had the same issues and setting the multi-value header flag to true resolved issues for me

You are a god, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants