节点 - 无法保存文件流
Node - Unable to save file stream
我正在使用 node post 将一些数据发送到外部服务,该服务应该会发回 PDF 以供保存,但我认为我没有正确完成任何一部分(我'我是节点的新手)。我查看了论坛并尝试了多种方法,但要么得到空白 PDF,要么得到损坏的 PDF。这是我用于请求的代码(以防我做错了),虽然我尝试使用 postman 调用服务并且我得到保存文件的提示,并且它有效,所以这肯定不是外部服务。:
var x = {//data to be sent}
var options = {
method: 'POST',
uri: '//link',
form: x,
headers: {
"Content-Type": "application/json",
'Authorization': 'Basic ' + new Buffer("user:pass").toString('base64')
}
};
request(options, function(error, response, body) {
//How to properly get the stream and save it as a valid PDF?
//I tried fs.witeFile, createWriteStream, pipe, and a bunch
//of other ways without luck.
});
这是我从外部服务得到的响应:
{
"statusCode": 200,
"body": "%PDF-1.4\n1 0 obj\n<<\n/Title (��)\n/Creato..{//very long response}..",
"headers": {
"x-powered-by": "Express",
"access-control-allow-origin": "*",
"vary": "Origin",
"connection": "close",
"content-type": "application/pdf",
"content-disposition": "inline; filename=\"report.pdf\"",
"file-extension": "pdf",
"number-of-pages": "1",
"x-xss-protection": "0",
"set-cookie": [
"session=_O2T27N......"
],
"date": "Thu, 21 Jan 2016 23:13:16 GMT",
"transfer-encoding": "chunked"
},
"request": {
"uri": {
"protocol": "https:",
"slashes": true,
"auth": null,
"host": "xxxxx.net",
"port": 443,
"hostname": "xxxxx.net",
"hash": null,
"search": null,
"query": null,
"pathname": "/api/report",
"path": "/api/report",
"href": "https://xxxxx.net/api/report"
},
"method": "POST",
"headers": {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic aXRA......",
"content-length": 129
}
}
}
如果有人知道如何正确获取和保存此文件,将不胜感激。
我预计您正在使用 request 模块,其中 returns 是一个流。您唯一需要做的就是将此流通过管道传输到一个文件中。这是通过以下方式完成的
request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'))
完整示例如下所示:
var options = {
method: 'POST',
body: JSON.stringify({ template: { recipe: 'phantom-pdf', engine: 'handlebars', content: 'Hello world'}}),
uri: 'http://localhost:3000/api/report',
headers: {
"Content-Type": "application/json",
'Authorization': 'Basic ' + new Buffer("admin:password").toString('base64')
}
};
request(options, function(error, response, body) {
}).pipe(fs.createWriteStream("report.pdf"))
您还可以检查 jsreport-client,这使得在 node.js 中更容易呈现远程报表。
我正在使用 node post 将一些数据发送到外部服务,该服务应该会发回 PDF 以供保存,但我认为我没有正确完成任何一部分(我'我是节点的新手)。我查看了论坛并尝试了多种方法,但要么得到空白 PDF,要么得到损坏的 PDF。这是我用于请求的代码(以防我做错了),虽然我尝试使用 postman 调用服务并且我得到保存文件的提示,并且它有效,所以这肯定不是外部服务。:
var x = {//data to be sent}
var options = {
method: 'POST',
uri: '//link',
form: x,
headers: {
"Content-Type": "application/json",
'Authorization': 'Basic ' + new Buffer("user:pass").toString('base64')
}
};
request(options, function(error, response, body) {
//How to properly get the stream and save it as a valid PDF?
//I tried fs.witeFile, createWriteStream, pipe, and a bunch
//of other ways without luck.
});
这是我从外部服务得到的响应:
{
"statusCode": 200,
"body": "%PDF-1.4\n1 0 obj\n<<\n/Title (��)\n/Creato..{//very long response}..",
"headers": {
"x-powered-by": "Express",
"access-control-allow-origin": "*",
"vary": "Origin",
"connection": "close",
"content-type": "application/pdf",
"content-disposition": "inline; filename=\"report.pdf\"",
"file-extension": "pdf",
"number-of-pages": "1",
"x-xss-protection": "0",
"set-cookie": [
"session=_O2T27N......"
],
"date": "Thu, 21 Jan 2016 23:13:16 GMT",
"transfer-encoding": "chunked"
},
"request": {
"uri": {
"protocol": "https:",
"slashes": true,
"auth": null,
"host": "xxxxx.net",
"port": 443,
"hostname": "xxxxx.net",
"hash": null,
"search": null,
"query": null,
"pathname": "/api/report",
"path": "/api/report",
"href": "https://xxxxx.net/api/report"
},
"method": "POST",
"headers": {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic aXRA......",
"content-length": 129
}
}
}
如果有人知道如何正确获取和保存此文件,将不胜感激。
我预计您正在使用 request 模块,其中 returns 是一个流。您唯一需要做的就是将此流通过管道传输到一个文件中。这是通过以下方式完成的
request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'))
完整示例如下所示:
var options = {
method: 'POST',
body: JSON.stringify({ template: { recipe: 'phantom-pdf', engine: 'handlebars', content: 'Hello world'}}),
uri: 'http://localhost:3000/api/report',
headers: {
"Content-Type": "application/json",
'Authorization': 'Basic ' + new Buffer("admin:password").toString('base64')
}
};
request(options, function(error, response, body) {
}).pipe(fs.createWriteStream("report.pdf"))
您还可以检查 jsreport-client,这使得在 node.js 中更容易呈现远程报表。