Reputation: 74
The code below returns "Got response: 301" in Lambda. I've tried this code in php, python, and now Node. Pasting this link into the browser returns JSON data as in this picture. How do I get the code to print out that same data? I need to end up putting the data into Mongo. I can get php and python to print the data locally but not in Lambda.
I think it has to do with the callback() shown here, and I'm trying to implement it.
var http = require('http');
var url = 'http://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=1min&apikey=demo';
exports.handler = function (event, context) {
http.get(url, function(res) {
console.log("Got response: " + res.statusCode);
res.on("data", function(chunk) {
console.log("BODY: " + chunk);
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
};
I updated the code to:
var http = require('http');
var url = 'http://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=1min&apikey=demo';
exports.handler = function (event, context) {
http.get(url, function(res) {
var data = '';
res.on('data', (chunk) => { data += chunk; });
res.on('end', () => { console.log("BODY: " + data); });
}).on('error', (e) => { console.log("Got error: " + e.message);});
};
And got this response:
START RequestId: 19a21615-7d09-11e7-93cc-cb3212ad23c5 Version: $LATEST 2017-08-09T13:46:10.102Z 19a21615-7d09-11e7-93cc-cb3212ad23c5 BODY: END RequestId: 19a21615-7d09-11e7-93cc-cb3212ad23c5 REPORT RequestId: 19a21615-7d09-11e7-93cc-cb3212ad23c5 Duration: 277.04 ms Billed Duration: 300 ms Memory Size: 128 MB Max Memory Used: 19 MB
Upvotes: 0
Views: 2522
Reputation: 756
The data received in chunks in order to print all the data you need to listen to 'end' event and then to log it. Try to appending the chunks on each data event and when the end event received log all the data.
var https = require('https');
var url = 'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=1min&apikey=demo';
exports.handler = function (event, context) {
https.get(url, function(res) {
var data = '';
res.on('data', (chunk) => { data += chunk; });
res.on('end', () => { console.log("BODY: " + data); });
}).on('error', (e) => { console.log("Got error: " + e.message);});
};
Upvotes: 7