CoinGecko-API icon indicating copy to clipboard operation
CoinGecko-API copied to clipboard

Coins list fetching api call fails with parse error

Open omeryousaf opened this issue 3 years ago • 1 comments

Description

I am using this library to scrape data from coingecko platform but i have recently started receiving the following error a lot

SyntaxError: Unexpected token e in JSON at position 0
at JSON.parse (<anonymous>)
at IncomingMessage.<anonymous> (/home/omer/abc-miner/node_modules/coingecko-api/lib/CoinGecko.js:864:25)
at IncomingMessage.emit (events.js:327:22)
at endReadableNT (internal/streams/readable.js:1327:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21)

on checking the source code at node_modules/coingecko-api/lib/CoinGecko.js:864 i find this

res.on('end', () => {
          try {
            body = Buffer.concat(body);
            body = body.toString();

            //Check if page is returned instead of JSON
            if (body.startsWith('<!DOCTYPE html>')) {
              Utils._WARN_('Invalid request', 'There was a problem with your request. The parameter(s) you gave are missing or incorrect.');
            } else if (body.startsWith('Throttled')) {
              Utils._WARN_('Throttled request', 'There was a problem with request limit.');
            }

            //Attempt to parse
>>            body = JSON.parse(body);    // this is the line referred to in the error stack i-e CoinGecko.js:864
          }
          catch (error) {
            reject(error);
          };
...

I use this way of fetching a list coins list, here is my code

try  {
  const CoinGecko = require('coingecko-api');
  const CoinGeckoClient = new CoinGecko();
  console.log('before calling CG api');
  let response = await CoinGeckoClient.coins.list();
  console.log('after calling CG api'); // this log rarely works
} catch(err) {
  console.log(err)
}

omeryousaf avatar May 16 '21 16:05 omeryousaf

I have the same issue when calling await CoinGeckoClient.coins.fetch()

vikinatora avatar Jun 21 '22 08:06 vikinatora