Ad
NodeJS - Converting Event.headers To Lowercase For Use With AWS
Is there any way by which I can convert the event.headers to lowercase?
I came across this thread but I can't get it to work correctly..
index.js
// I have tried 2 different attempts, one is my own and most probably wrong,
// the other is from the link I posted above.
// My obviously flawed attempt
function makeLower(headers) {
for(const key in headers) {
event.headers[key] = key.toLowerCase();
}
}
// console.log('Distribution Audit Create Invoked');
const headers = makeLower(event.headers);
// Convert headers to lower case so AWS doesn't kick off
// Source: https://github.com/serverless/serverless/issues/2765
// const headers = {};
// for (const key in event.headers) {
// headers[key.toLowerCase()] = event.headers[key];
// }
// event.headers = headers;
console.log('HEADERS: ' + headers['content-type']);
index.test.js (testing isn't my strongest suit :-/)
it('should still return 202, with uppercase headers >
converting them to lower', done => {
// Isn't working when sending in CAPS - Does work with non-caps
event.headers = {'content-type': 'SOME HEADERS'};
lambda.handler(event, null, (err, request) => {
should.not.exist(err);
should.exist(request);
done();
});
})
Ad
Answer
Shouldn't your function convert both the keys and the values to lowercase?
function makeLower(headers) {
let head = {}
for(const key in headers) {
if (headers.hasOwnProperty(key)) {
head[key.toLowerCase()] = headers[key].toLowerCase()
}
}
return head
}
And not use the original object to store the lowercase values? Otherwise you end up with both the lowercase and original version in your object.
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad