我有一个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);
});
现在我正在尝试使用此数据实际做两件事。
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();
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响应是完美的,因为当
null
reqClient.encoding(null)