Skip to content

Instantly share code, notes, and snippets.

@Franco-Poveda
Created March 7, 2018 22:22
Show Gist options
  • Select an option

  • Save Franco-Poveda/74a938547daa5d2f1053b845a51aa862 to your computer and use it in GitHub Desktop.

Select an option

Save Franco-Poveda/74a938547daa5d2f1053b845a51aa862 to your computer and use it in GitHub Desktop.
https request using lambda and nodejs
'use strict';
const https = require('https');
/**
* Pass the data to send as `event.data`, and the request options as
* `event.options`.
* Will succeed with the response body.
*/
exports.handler = (event, context, callback) => {
const req = https.request(event.options, (res) => {
let body = '';
console.log('Status:', res.statusCode);
console.log('Headers:', JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', (chunk) => body += chunk);
res.on('end', () => {
console.log('Successfully processed HTTPS response');
// If we know it's JSON, parse it
if (res.headers['content-type'] === 'application/json') {
body = JSON.parse(body);
}
callback(null, body);
});
});
req.on('error', callback);
req.write(JSON.stringify(event.data));
req.end();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment