0

So I've been trying to take some json info out of an API, but when I try to take the JSON object and assign it to an variable, it just doesn't work.

 var info = [];

https.get('https://br1.api.riotgames.com/lol/summoner/v4/summoners/by-name/sumname?api_key=',
(res) => {
  info.push(res.toString());
  
 res.on('data', (d) => {
  info.push(d.toString());
 });
 
 res.on('end', () =>{
   console.log(info);
 });
});

console.log(info);
<code>
  • listen for the data event on response object, push each chunk in the array, wait for the end event and build out of the array chunks a complete response with `info.concat().toString()` its asynchron, so you have to wait the response is completed. `https.get` returns a request stream, you have to call `.end()` on it to complete/end the request and start receiving the response. – Marc Oct 09 '20 at 19:57

1 Answers1

-1

I actually had a similar issue recently when i started making GET API Calls. what i suspect the issue is -- https.get will return a promise. You need to resolve the promise before you can obtain the data within. Refer to the following post from 31piy

Use result of HTTPS GET request [Node.js]

This may prove helpful!

James Bell
  • 11
  • 4