2015-09-24 14 views
9

Come posso inviare la seguente query nel set di caratteri win1251?Node.js: Invia richiesta http in charset win1251

var getData = querystring.stringify({ 
     type: "тест", note: "тест1" 
    }), 
    options = { 
     host: config.host, 
     path: config.path + '?' + getData, 
     method: 'GET' 
    }; 

http.request(options, function (res) {...}).end(); 
+0

Non è possibile inviare qualsiasi cosa "in un certo charset". Puoi codificarlo solo in quel set di caratteri (in binario) e quindi inviarlo nel protocollo scelto (come binario!). Quindi, al server ricevente possono essere fornite informazioni per decodificare il file binario nel set di caratteri corretto su una stringa. È necessario fornire maggiori informazioni sui vincoli dati e sul risultato desiderato. –

risposta

4

penso che questo frammento può aiutare a

request({ 
uri: website_url, 
method: 'GET', 
encoding: 'binary' 
}, function (error, response, body) { 
    body = new Buffer(body, 'binary'); 
    conv = new iconv.Iconv('windows-1251', 'utf8'); 
    body = conv.convert(body).toString(); 
    } 
}); 

Update 1

OK, penso che trovare qualcosa di utile :)

Please check out this link

È possibile utilizzare sopra utility come questa

// Suppose gbkEncodeURIComponent function already exists, 
// it can encode string with `gbk` encoding 
querystring.stringify({ w: '中文', foo: 'bar' }, null, null, 
    { encodeURIComponent: win2unicode }) 
// returns 
'w=%D6%D0%CE%C4&foo=bar' 
+2

Ho bisogno che la stringa di query debba essere codificata in win1251 – ollazarev

2

Il server accetta realmente win1251 nella parte di query dell'URL?

What character set should I assume the encoded characters in a URL to be in?

ma qui ci sono alcuni così risposte che corrispondono alla vostra domanda:

Converting from Windows-1251 to UTF-8 in Node.js

nodejs http response encoding

che riducono fino al utilizzando una di queste librerie, che si dovrebbe anche trovare su NPM.

https://github.com/bnoordhuis/node-iconv

o

https://github.com/ashtuchkin/iconv-lite

Michael

Problemi correlati