简体   繁体   中英

Chunks are missing while streaming the response data in Node.js

After getting the response from axios, I am converting it into stream.After getting some chunks stream.on("end" is executing. Due to this I am getting

Uncaught SyntaxError: Unexpected token with JSON.parse

For the normal data (response from the API) it's working. But for huge responses the chunks are missing.

I also tried to save the chunks in array, but no use. For the same API I tried with Postman and I am getting the response.

  httpRequest["responseType"] = "stream"
  httpRequest["responseEncoding"] = "utf8"
  returnValue = await axios(httpRequest)
  let outputString = "";
  const stream = returnValue.data;
  stream.on("data", (chunk) => {
       outputString += chunk.toString("utf8")
  });
  stream.on("end", () => {
  var finalJson = JSON.parse(outputString);
  });

After adding Accept-Encoding as gzip, I am getting the complete response without using streams.

httpRequest.headers["Accept-Encoding"] = "gzip, deflate, br"
returnValue = await axios(httpRequest)
console.log(JSON.parse(JSON.stringify(returnValue.data)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM