2015-10-05 16 views
7

che sto facendo la seguente richiesta (mediante richiesta/richiesta) contro un servizio Web:richiesta Node.js trasforma dieresi a

return request.postAsync({ 
    url, 
    charset: 'Cp1252', // I also tried utf-8 
    encoding: null, // 
    // I also tried Cp1252 -> unknown encoding, 
    // I also tried utf-8 and nothing at all 
    headers: { 
     "Accept": "application/octet-stream, text, text/plain, text/xml", 
     "Accept-Encoding": "UTF-8", 
     'Content-Type': "text/plain; charset=Cp1252;", // also tried utf-8, ISO-8859-1 
     "User-Agent": "me" 
    } 
}).spread((res, body) => { 
    body = body.toString(); // I also tried without toString(); 
    let ws = fs.createWriteStream('hhh.csv'); 
    ws.write(body); 
    ws.end(); 

Qualsiasi cosa io faccia, dieresi si trasformano in .

Queste sono le intestazioni del servizio Web invia indietro:

'content-type': 'text; charset=Cp1252', 
'content-length': '1895980', 
vary: 'Accept-Encoding,User-Agent' 

sto cercando questo per giorni senza fortuna a tutti. Che cosa sto facendo di sbagliato?

Ecco un elenco di domande/risposte che non ha risolto il mio problema finora:

Può essere che una delle seguenti cause mia stringa di input a non essere UTF-8?

let hash = crypto.createHmac("sha256", this.options.Signer); 
this.query['Signature'] = hash.update(stringToSign).digest("base64"); 

firmatario è una stringa contenente 0-9, a-z, A-Z, + e /.

this.query['Signature'] è parte dell'URL.

+0

Quali sono i caratteri esatti che si inviano? In che modo sono attualmente codificati e qual è il set di caratteri corrente per loro? – zerkms

+0

Nel luogo in cui l'umlaut è "umlaut". – zerkms

+0

"la richiesta di nodo js trasforma le dieresi a " "Qualsiasi cosa io faccia, le dieresi sono trasformate in ." --- cosa significano queste frasi? – zerkms

risposta

7

L'ho finalmente risolto, utilizzando iconv-lite e impostando la codifica della richiesta¹ su null, facendo in modo che restituisca il corpo come Buffer. Ecco la mia configurazione ora funzionante:

return request.getAsync({ 
     url, 
     encoding: null, 
     headers: { 
      "Accept": "text, text/plain, text/xml", 
      "Accept-Encoding": "UTF-8", 
      'Content-Type': "text/plain; charset=utf-8;", 
      "User-Agent": "me" 
     } 
    }).spread((res, body) => { 
     let a = iconv.decode(new Buffer(body), 'cp1252'); 
     // now a is holding a string with correct Umlauts and ß 
+0

http://www.utf8everywhere.org/ – zerkms

+1

correlati: http://stackoverflow.com/questions/12040643/nodejs-encoding-using-request –