2017-01-02 1 views
0

Ich entwickle ein Tool, das ttf font in andere Formate umwandelt, indem ich onlinefontconverter.com API benutze. So habe ich ein Problem mit dem Erhalten von propper tar.gz Archiv. Ich bekomme Datei, aber OS sagte mir, dass das Archiv beschädigt ist. Wie kann ich die Datei vom Antworttext speichern? Hier ist der Code i verwendet:Wie bekomme ich das .tar.gz-Archiv von der einfachsten Antwort?

'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'}); 
    }); 

hier Header ist:

{ '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: Ich habe ein Problem mit unirest, also habe ich neu geschrieben auf einfache Anfrage.

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'));

Antwort

0

Ich denke, dass dies ein Problem einer falschen Codierung der Antwort in unirest Bibliothek ist. Versuchen Sie so etwas (und verstecken Sie Ihren geheimen Schlüssel):

'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'); 
     }); 
+0

Danke! Ich habe es selbst herausgefunden. Mein Code ist im ersten Post. –

Verwandte Themen