serverless-express
serverless-express copied to clipboard
Double encoding of query
seems that query is encoded one more time when hitting express. I had to add this peace of code before passing event to module:
exports.handler = (event, context) => {
Object.keys(event.queryStringParameters).forEach((key) => {
// eslint-disable-next-line no-param-reassign
event.queryStringParameters[key] = decodeURIComponent(event.queryStringParameters[key]);
});
awsServerlessExpress.proxy(server, event, context);
};
Thanks. I'll need to verify this and fix as part of v4.
Hi, just saw this. I think it's the same problem I described in #241 .
@karlismelderis were you using Application Load Balancer by any chance?
I'd just like to chime in here and say I'm also running into this as an issue. In additional to the decoding, it's important to note that the + character has special behaviour in query strings - it decodes to a space character, but not if you use decodeURIComponent, so in the example above you'll still be getting + characters back when you shouldn't.
Here's the solution I've been using, which effectively rebuilds the whole URL and then parses it again using the querystring module:
const querystring = require('querystring');
exports.handler = (event, context) => {
const qs = Object.keys(event.queryStringParameters).map(key => key + '=' + event.queryStringParameters[key]).join('&');
event.queryStringParameters = querystring.parse(qs);
ase.proxy(server, event, context);
};
That said, I'm pretty sure the behaviour of not decoding keys and values is a bug, both because of how it's inconsistent with API Gateway, and how it can't cope with certain characters (e.g. ?foo%25bar=baz apparently decodes as { foo: "" } which is obviously wrong) so perhaps this needs to be addressed at a higher level inside the ALB-Lambda handler itself rather than inside this module