如何从 unirest 响应中获取 .tar.gz 存档?
How to get .tar.gz archive from unirest response?
我使用 onlinefontconverter.com API 开发了一个将 ttf 字体转换为其他格式的工具。所以我在获取 propper tar.gz 存档时遇到了问题。我得到文件,但 OS 告诉我存档已损坏。那么如何从响应 body 中保存文件呢?这是我使用的代码:
'use strict';
const unirest = require('unirest');
const fs = require('fs');
const file = fs.createWriteStream('./onlinefontconverter.com.tar.gz');
unirest.post("https://ofc.p.mashape.com/directConvert/")
.header("X-Mashape-Key", "key")
.attach("file", fs.createReadStream('./Roboto-Thin.ttf'))
.field("format", "svg")
.end((result) => {
file.write(result.body, {encoding:'binary'});
});
这里是headers:
{ 'accept-ranges': 'bytes',
'access-control-allow-headers': 'X-Mashape-Key',
'access-control-allow-methods': 'POST, OPTIONS',
'access-control-allow-origin': '*',
'access-control-expose-headers': 'Content-Disposition',
'cache-control': 'public, max-age=0',
'content-disposition': 'attachment; filename="onlinefontconverter.com.tar.gz"',
'content-type': 'application/octet-stream',
date: 'Mon, 02 Jan 2017 18:52:34 GMT',
expires: '0',
'last-modified': 'Mon, 02 Jan 2017 18:52:34 GMT',
pragma: 'no-cache',
server: 'Mashape/5.0.6',
via: '1.1 vegur',
'content-length': '119251',
connection: 'Close' }
更新:
我对 unirest 有疑问,所以我根据简单的要求重写了。
const file = fs.createWriteStream('./fonts.tar.gz');
const request = require('request');
var r = request.post({
url: 'https://ofc.p.mashape.com/directConvert/',
headers: {'X-Mashape-Key' : 'key'},
}, function(error, response, body){
// console.log(body);
// console.log(response.statusCode);
// console.log(response.headers);
});
r.pipe(file);
var form = r.form();
form.append('format', 'ttf');
form.append('my_file', fs.createReadStream('Aller_Rg.ttf'));
我认为这是 unirest
库中响应编码不正确的问题。尝试这样的事情(并隐藏您的密钥):
'use strict';
const fs = require('fs');
const request = require('request');
const formData = {
format: 'svg',
file: fs.createReadStream('./Roboto-Thin.ttf')
}
var buff = [];
request.post({ url: "https://ofc.p.mashape.com/directConvert/",
formData: formData,
headers: {
"X-Mashape-Key": "SecretMashapeKey"
}
})
.on('data', function(chunk) {
buff.push(chunk);
})
.on('end', function() {
var data = Buffer.concat( buff );
fs.writeFileSync( 'onlinefontconverter.com.tar.gz',
data,
'binary' );
});
我使用 onlinefontconverter.com API 开发了一个将 ttf 字体转换为其他格式的工具。所以我在获取 propper tar.gz 存档时遇到了问题。我得到文件,但 OS 告诉我存档已损坏。那么如何从响应 body 中保存文件呢?这是我使用的代码:
'use strict';
const unirest = require('unirest');
const fs = require('fs');
const file = fs.createWriteStream('./onlinefontconverter.com.tar.gz');
unirest.post("https://ofc.p.mashape.com/directConvert/")
.header("X-Mashape-Key", "key")
.attach("file", fs.createReadStream('./Roboto-Thin.ttf'))
.field("format", "svg")
.end((result) => {
file.write(result.body, {encoding:'binary'});
});
这里是headers:
{ 'accept-ranges': 'bytes',
'access-control-allow-headers': 'X-Mashape-Key',
'access-control-allow-methods': 'POST, OPTIONS',
'access-control-allow-origin': '*',
'access-control-expose-headers': 'Content-Disposition',
'cache-control': 'public, max-age=0',
'content-disposition': 'attachment; filename="onlinefontconverter.com.tar.gz"',
'content-type': 'application/octet-stream',
date: 'Mon, 02 Jan 2017 18:52:34 GMT',
expires: '0',
'last-modified': 'Mon, 02 Jan 2017 18:52:34 GMT',
pragma: 'no-cache',
server: 'Mashape/5.0.6',
via: '1.1 vegur',
'content-length': '119251',
connection: 'Close' }
更新: 我对 unirest 有疑问,所以我根据简单的要求重写了。
const file = fs.createWriteStream('./fonts.tar.gz');
const request = require('request');
var r = request.post({
url: 'https://ofc.p.mashape.com/directConvert/',
headers: {'X-Mashape-Key' : 'key'},
}, function(error, response, body){
// console.log(body);
// console.log(response.statusCode);
// console.log(response.headers);
});
r.pipe(file);
var form = r.form();
form.append('format', 'ttf');
form.append('my_file', fs.createReadStream('Aller_Rg.ttf'));
我认为这是 unirest
库中响应编码不正确的问题。尝试这样的事情(并隐藏您的密钥):
'use strict';
const fs = require('fs');
const request = require('request');
const formData = {
format: 'svg',
file: fs.createReadStream('./Roboto-Thin.ttf')
}
var buff = [];
request.post({ url: "https://ofc.p.mashape.com/directConvert/",
formData: formData,
headers: {
"X-Mashape-Key": "SecretMashapeKey"
}
})
.on('data', function(chunk) {
buff.push(chunk);
})
.on('end', function() {
var data = Buffer.concat( buff );
fs.writeFileSync( 'onlinefontconverter.com.tar.gz',
data,
'binary' );
});