Created
March 7, 2018 22:22
-
-
Save Franco-Poveda/74a938547daa5d2f1053b845a51aa862 to your computer and use it in GitHub Desktop.
https request using lambda and nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| '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