[英]Node - Unable to save file stream
I'm using node to post some data to an external service which is supposed to send me back a PDF to save, but I don't think I'm doing either part correctly (I'm new to node). 我正在使用node将一些数据发布到外部服务,该服务应该将其发送回PDF以保存,但是我认为我做的任何一部分都不正确(我是Node的新手)。 I've looked on forums and tried a dozen ways but I either get a blank PDF or a corrupt one.
我浏览过论坛并尝试了十几种方法,但是我得到的是空白PDF或损坏的PDF。 Here is the code I'm using for the request (in case I'm doing it wrong), although I tried using postman call the service and I get a prompt to save the file, and it works, so it's not the external service for sure.:
这是我用于请求的代码(以防万一我做错了),尽管我尝试使用邮递员调用该服务,并提示保存文件,并且它可以工作,所以它不是外部服务当然。:
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.
});
Here is the response I get from the external service: 这是我从外部服务获得的响应:
{
"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
}
}
}
If anyone knows how to properly get and save this file, it would be appreciated. 如果有人知道如何正确获取和保存此文件,将不胜感激。
I expect you are using request module which returns a stream. 我希望您使用的是返回流的请求模块。 The only thing you need to do is pipe this stream into a file.
您唯一需要做的就是将流传输到文件中。 This is done in the following way
这是通过以下方式完成的
request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'))
The full example can then look like this: 完整的示例如下所示:
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"))
You may also check jsreport-client which makes remote report rendering easier in node.js. 您还可以检查jsreport-client ,这使node.js中的远程报告呈现更加容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.