我收到了多个pdf文件,必须从REST-API下载。
在身份验证和连接之后,我尝试使用request-promise下载文件:
const optionsStart = {
uri: url,
method: 'GET',
headers: {
'X-TOKEN': authToken,
'Content-type': 'applcation/pdf'
}
}
request(optionsStart)
.then(function(body, data) {
let writeStream = fs.createWriteStream(uuid+'_obj.pdf');
console.log(body)
writeStream.write(body, 'binary');
writeStream.on('finish', () => {
console.log('wrote all data to file');
});
writeStream.end();
})
请求创建一个pdf (大约1-2MB),但我无法打开它。(Mac Preview显示空白页面和adobe show =>
打开此文档时出错。读取此文档(14)时出现问题。
我没有关于下载文件的API端点的信息。只做这个卷发:
curl -o doc.pdf --header "X-TOKEN: XXXXXX"
http://XXX.XXX/XXX/docs/doc1
我的错误在哪里?
更新:
我在编辑模式下打开文件,文件看起来是这样的:
没有任何经验:-)
发布于 2018-02-13 02:45:44
在你的请求选项中添加编码:'binary‘:
const optionsStart = {
uri: url,
method: "GET",
encoding: "binary", // it also works with encoding: null
headers: {
"Content-type": "application/pdf"
}
};
发布于 2018-10-20 22:58:26
将encoding: null
添加到您的请求选项:
const optionsStart = {
uri: url,
method: "GET",
encoding: null,
headers: {
"Content-type": "application/pdf"
}
};
然后,将响应转换为缓冲区(如果需要):
const buffer = Buffer.from(response);
发布于 2020-02-21 03:31:25
尝尝这个
const optionsStart = {
uri: url,
method: 'GET',
headers: {
'X-TOKEN': authToken,
'Content-type': 'application/pdf'
},
encoding: null
}
request(optionsStart, (err, resp) => {
let writeStream = fs.createWriteStream(uuid + '_obj.pdf');
writeStream.write(resp.body, 'binary');
writeStream.on('finish', () => {
console.log('wrote all data to file');
});
writeStream.end();
})
https://stackoverflow.com/questions/48752822
复制相似问题