节点:Express:如何处理应用程序/八位字节流; charset =; UTF-8响应?

问题描述 投票:1回答:1

我有一个node-express应用程序。在那里,我正在尝试调用一个API,该API将原始xlsx对象响应为

'Content-Type':'application / octet-stream; charset =; UTF-8'

编码我如何调用API:

var unirest = require("unirest");

var reqClient = unirest("POST", "https://api.application.com/getExcel");
reqClient.headers({ "Authorization": "Bearer " + req.session.passport.user.token, 
"content-type": req.headers['content-type'], "application/json"});
reqClient.type("json");
reqClient.send(JSON.stringify(requestbody));
reqClient.end(function(res) {
if (res.error) throw new Error(res.error);
console.log(res.body);
});

现在我正在尝试使用此数据实际做两件事。

  1. 将其写入Excel文件。下面是我如何尝试的代码:
    let data = res.body // res is the response coming from the API
    let buf = Buffer.from(data);
    excelfile = fs.createWriteStream("result.xlsx");
    excelfile.write(buf);
    excelfile.end();
  1. 尝试将其发送到UI,将在其中创建excelfile。下面是我的代码:
    let data = res.body // res is the response coming from the API
    let buf = Buffer.from(data);
    response.write(buf); //response is the response to the request to ui
    response.end();

所以在两种情况下文件都已损坏。

但是API响应是完美的,因为当

我有一个node-express应用程序。在那里,我正在尝试调用将原始xlsx对象作为“ Content-Type”响应的API:'application / octet-stream; charset =; UTF-8'编码方式...] >
node.js express utf-8 content-type
1个回答
1
投票
[当处理二进制数据时,必须将编码设置为null

reqClient.encoding(null)

© www.soinside.com 2019 - 2024. All rights reserved.