在得到axios的响应之后,我将它转换为流。在得到一些块后,stream.on("end"
正在执行。因此,我得到了
Uncaught SyntaxError: Unexpected token with JSON.parse
对于正常数据(来自API的响应),它正在工作。但是对于巨大的反应来说,大块的缺失。
我也尝试将数据块保存在数组中,但没有用。对于我使用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);
});
将Accept-Encoding作为gzip添加后,我在不使用流的情况下获得完整的响应。
httpRequest.headers["Accept-Encoding"] = "gzip, deflate, br"
returnValue = await axios(httpRequest)
console.log(JSON.parse(JSON.stringify(returnValue.data)))