I'm working on a small discord bot which requires me to do some JavaScript. I know there is some alternatives but I want to use JavaScript because of reasons. The thing is I'm trying to make a get request for JSON in node.js but It only seems to work if I put the data (chunk) into the console. If I try to concat the data I just get an empty string.
Here is my code:
var https = require('https');
var output;
var options = {
host: '---',
port: 443,
method: 'GET',
// json: true,
path: '/post/index.json?limit=15',
headers: { 'user-agent': 'DiscordLucario/1.0' }
};
var req = https.request(options, function (res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
//console.log(chunk);
output += chunk;
});
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.on('end', function () {
console.log(output);
GTwebObject = JSON.parse(output);
GTpictureIndex = getRandomInt(1, GTwebObject.length);
GTpictureTags = GTwebObject[GTpictureIndex].tags;
GTpictureURL = GTwebObject[GTpictureIndex].file_url;
});
req.end();