0

I am trying to console the data from "https://pokeapi.co/api/v2/pokemon" using HTTPS GET request but the console gives the following error: Error I am getting while making the GET request

This is my code: Code

I make the use of HTTPS Get request method to get the data and was expecting to get the JSON but couldn't.

  • `res.on('data',..` gets the response in chunks, you need to capture them all and concatenate, you will get a `res.on('end', ` when all chunks are loaded. – Keith Dec 20 '22 at 17:14

1 Answers1

0

res.on('data') provides a stream of data. This means that it sends parts of the request one at a time. When using the https lib you could add the responses one by one to a string and then parsing everything in res.on('end'). I would suggest though using an easier HTTP library, for instance I really like using axios. If you cannot figure out how to use res.on('data' and res.on('end' let me know and I'll type up an example for your situation.

Found the answer here: How to get data out of a Node.js http get request

Lars Vonk
  • 623
  • 5
  • 13
  • 1
    It's also pretty much in the Docs -> https://nodejs.org/en/docs/guides/anatomy-of-an-http-transaction/ – Keith Dec 20 '22 at 17:16
  • Thank you for answering. An example would help me a lot in figuring out what you are trying to convey. – govind bhardwaj Dec 20 '22 at 17:45
  • Sorry for the late reaction, it was night for me. Well I think in your case what would work is: `https.get(url, res => { let result = ''; res.on('data', data => { result += data; }); res.on('end', () => { const pokemon = JSON.parse(result); }); console.log(pokemon); });` – Lars Vonk Dec 21 '22 at 10:40
  • Nice! Could you mark this answer as the correct one, for my rep :-) – Lars Vonk Dec 21 '22 at 15:07