0

Im trying to download a json response from an API and it fails like 9 out of 10 times.

The response is quite large - roughly 90mb. Downloading in postman, webbrowser works every time, but in node the response seems to be distorted and not a valid json. Sometimes it works.

Tried both v16.1.0 and v14.15.5 using node-fetch-2.6.1

const fetch = require('node-fetch');

fetch('https://xxxxx', { 
        headers: { 'Authorization': 'Basic xxxxxx',
     },
   })

   .then(res => res.json())
    .then(json => console.log(json));

Any ideas?

  • When you try it in the browser, did you use the exact same program? (but without the import) – Alex028502 May 12 '21 at 18:42
  • Sorry was unclear - I can show the json in the browser and save without it beeing garbled. Somehow it gets reformatted in node.js. – Mikael Hugo May 12 '21 at 19:56
  • What happens when you put `fetch('https://xxxxx', { headers: { 'Authorization': 'Basic xxxxxx'}}).then(res => res.json()).then(json => console.log(json));` into the browser console? Does that work over and over? – Alex028502 May 12 '21 at 20:00
  • Yes that works over and over – Mikael Hugo May 12 '21 at 23:14
  • OK. Since nobody how knows the answer seems to have noticed this question, I would suggest rewriting the above test script with https://www.npmjs.com/package/axios to find out if `node-fetch` itself is to blame, or something deeper down – Alex028502 May 13 '21 at 09:35
  • 1
    Axios gives same issue. I solved it using node-libcurl instead. – Mikael Hugo May 13 '21 at 15:35

1 Answers1

0

Suspected bug in node-fetch - solved using node-libcurl.

Alex028502
  • 3,486
  • 2
  • 23
  • 50