I develop a tool converting ttf font to other formats using onlinefontconverter.com API. So I have a problem with obtaining propper tar.gz archive. I get file, but OS told me that archive is damaged. So how can I save file from response body? Here is code i used:
'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'});
});
Here is 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' }
UPD: I've a problem with unirest, so I've rewritten on simple request.
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'));
I think that this is a problem of incorrect encoding of response in
unirest
library. Try something like this (and hide your secret key):