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
Parse json body when content-type header is lower-cased #75
Conversation
Codecov Report
@@ Coverage Diff @@
## master #75 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 10 10
Lines 165 167 +2
Branches 37 39 +2
=====================================
+ Hits 165 167 +2
Continue to review full report at Codecov.
|
That it's definitely interesting. At a first glance, it would look more like an issue with the current client making the request, rather than some weird internals. Let me run some tests to try to clarify when this happens. Meanwhile, how are you generating the requests to your lambdas? |
It seems that API Gateway does not normalize incoming headers and they are transferred exactly with the casing specified by the client (see attached screenshot). If the client pass At the moment I would be against merging this because it tries to prevent an error in the client. I would instead encourage a client to follow the standard with care and submit headers with the expected casing. With that being said I'd love an opinion from the other members of the team: @DavidWells @peterjcaulfield @dkatavic @padraigobrien @techfort @acambas |
Looking at the specification RFC7230 Implementing this check properly isn't easy in JS, we would need to get all the keys of the headers and check is any of them matching the value Is there a possibility that we can have multiple |
Considering what @dkatavic just said it might make sense to address the issue at middy's level. Also, as a separate matter, we should consider the case when the I will have a thought about these matters during the weekend and try to come up with a solution. |
After thinking about this for a while, I think the best solution, in order to keep things fast and simple, is to make middlewares check only the canonical representation of a header. If in a given application the header might come with another casing (eg. lowercase), you can easily write an additional middleware that takes care of the serialization as in the (untested) example below: middy(handler)
.before((handler, next) => {
if (!handler.event.headers['Content-Type'] && handler.event.headers['content-type']) {
handler.event.headers['Content-Type'] = handler.event.headers['content-type']
delete handler.event.headers['content-type']
}
return next()
})
.use(jsonBodyParser()) Of course, you can create your own reusable middleware based on this example if you need this feature in multiple handlers. @vladgolubev Based on this reasoning, I am closing this PR, but feel free to comment here if you have more questions/concerns. |
Looks like a strange exception. Didn't work for me without this fix
See https://serverless.com/framework/docs/providers/aws/events/apigateway#example-lambda-proxy-event-default