-
Notifications
You must be signed in to change notification settings - Fork 556
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
API Gateway Proxy: headers are case sensitive #117
Comments
Any progress on this? Thanks! |
|
For a workaround using the stdlib, adjusted using #117 (comment) below. import (
"context"
"net/http"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
func HandleRequest(_ context.Context, input events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
headers := http.Header{}
for header, values := range request.MultiValueHeaders {
for _, value := range values {
headers.Add(header, value)
}
}
foo := headers.Get("Foo")
// ...
}
func main() {
lambda.Start(HandleRequest)
} |
@Crazometer: Heads up, you might encounter a problem when one of your headers is lowercased. Let's take a following unit test as an illustrative example: package lambda_test
import (
"net/http"
"testing"
"github.com/aws/aws-lambda-go/events"
"github.com/stretchr/testify/assert"
)
func TestNewRequest_header(t *testing.T) {
e := events.APIGatewayProxyRequest{
MultiValueHeaders: map[string][]string{
"Content-Type": {"application/json"},
"host": {"localhost"},
"X-Foo": {"bar"},
},
}
headers := http.Header(e.MultiValueHeaders)
assert.Equal(t, `application/json`, headers.Get("Content-Type"))
assert.Equal(t, `application/json`, headers.Get("content-type"))
assert.Equal(t, `localhost`, headers.Get("Host"))
assert.Equal(t, `localhost`, headers.Get("host"))
assert.Equal(t, `bar`, headers.Get("x-Foo"))
assert.Equal(t, `bar`, headers.Get("X-foo"))
} If I would run this test I will encounter two errors and following output: === RUN TestNewRequest_header
--- FAIL: TestNewRequest_header (0.00s)
./request_test.go:25:
Error Trace: request_test.go:25
Error: Not equal:
expected: "localhost"
actual : ""
Diff:
--- Expected
+++ Actual
@@ -1 +1 @@
-localhost
+
Test: TestNewRequest_header
./request_test.go:26:
Error Trace: request_test.go:26
Error: Not equal:
expected: "localhost"
actual : ""
Diff:
--- Expected
+++ Actual
@@ -1 +1 @@
-localhost
+
Test: TestNewRequest_header
FAIL
FAIL lambda_test 0.003s
Error: Tests failed. I am author of the https://github.com/piotrkubisa/apigo, a package that behaves like L7 proxy for APIG, and for aforementioned "issue" I used the loop with a
|
@piotrkubisa Thanks, that's fairly counter intuitive to me but makes sense with the example. Updated the workaround snippet! New test package main
import (
"net/http"
"testing"
"github.com/aws/aws-lambda-go/events"
"github.com/stretchr/testify/assert"
)
func TestNewRequest_header(t *testing.T) {
e := events.APIGatewayProxyRequest{
MultiValueHeaders: map[string][]string{
"Content-Type": {"application/json"},
"host": {"localhost"},
"X-Foo": {"bar"},
},
}
headers := http.Header{}
for header, values := range e.MultiValueHeaders {
for _, value := range values {
headers.Add(header, value)
}
}
assert.Equal(t, `application/json`, headers.Get("Content-Type"))
assert.Equal(t, `application/json`, headers.Get("content-type"))
assert.Equal(t, `localhost`, headers.Get("Host"))
assert.Equal(t, `localhost`, headers.Get("host"))
assert.Equal(t, `bar`, headers.Get("x-Foo"))
assert.Equal(t, `bar`, headers.Get("X-foo"))
}
//PASS
//ok scratch 0.016s |
I'd like to add that the same problem occurs when accessing headers from the Generally, I use the gin framework, which exposes a What are the odds that these AWS structs could be augmented with a similar method? |
Any progress? |
Accessing the headers from an
APIGatewayProxyRequest
event asrequest.Headers["Authorization"]
makes them case sensitive. HTTP headers are case insensitive by definition. I think it would make sense to have a getter method for them like the one innet/http
where case sensitivity would be handled.The text was updated successfully, but these errors were encountered: