9

How can I send the following query in win1251 charset?

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

http.request(options, function (res) {...}).end();
ollazarev
  • 1,076
  • 8
  • 26
  • You can not send anything "in a certain charset". You can only encode it in that charset (to binary) and then send it in the chosen protocol (as binary!). Then, the receiving server can be given information to decode the binary in the correct charset to a string. You need to provide more info about your given restraints and desired outcome. – Igor Skoric Jan 04 '16 at 13:13

2 Answers2

5

I think this snippet can help you

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, i think find something useful :)

Please check out this link

You can use above utility like this

// 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'
Alireza Davoodi
  • 749
  • 7
  • 20
2

Does the server really accept win1251 in the query part of the URL?

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

But here are some SO answers that match your question:

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

nodejs http response encoding

Which reduce down to using either of these libraries, which you should also find on npm.

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

or

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

Michael

Community
  • 1
  • 1
MichaelStoner
  • 889
  • 10
  • 26