如何保存作为application / pdf返回的响应 [英] How to save response returned as application/pdf
本文介绍了如何保存作为application / pdf返回的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在对SmartSheet进行API调用,将工作表作为PDF文件返回。
I am making an API call to SmartSheet that returns the worksheet as a PDF file.
这是相关文档 - 链接
This is the relevant documentation - link
我的问题是如何接受PDF响应并在nodeJs中本地保存?我正在使用 https
模块,我知道如何发出请求,但我无法理解如何接受响应:
My question is how do I accept the PDF response and save it locally in nodeJs? I am making use of the https
module and I know how to make the request, but I can't understand how do I accept the response:
https.request(options, function (response) {
var body = '';
response.on('data', function (chunk) {
body += chunk;
});
response.on('end', function () {
//What do I do with the body here?
});
});
推荐答案
这取决于。您想如何存储下载的PDF?如果要将它们存储在本地文件系统中,则可以直接将数据流式传输到文件中。
That depends. How do you want store downloaded PDFs? If you want to store them in local file system, then you can stream data directly into the file.
例如:
var fs = require('fs');
var https = require('https');
var options = {
hostname: 'google.com',
port: 443,
path: '/',
method: 'GET'
};
var req = https.request(options, function (response) {
response.on('end', function () {
// We're done
});
response.pipe(fs.createWriteStream('/path/to/file'));
});
req.end();
req.on('error', function (err) {
// Handle error here
});
这篇关于如何保存作为application / pdf返回的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文