简体   繁体   English

在Node.js中流式传输响应数据时缺少块

[英]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 从axios获得响应后,我将其转换为流。获得一些块stream.on("end"正在执行。由于这个原因,我得到了

Uncaught SyntaxError: Unexpected token with JSON.parse

For the normal data (response from the API) it's working. 对于正常数据(来自API的响应),它可以正常工作。 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. 对于与Postman尝试使用的相同API,我得到了响应。

  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. 将Accept-Encoding添加为gzip后,我得到了完整的响应,而没有使用流。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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