最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js: Send http request in win1251 charset - Stack Overflow

programmeradmin3浏览0评论

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

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();
Share asked Sep 24, 2015 at 8:13 ollazarevollazarev 1,1061 gold badge9 silver badges26 bronze badges 1
  • 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 oute. – Igor Skoric Commented Jan 4, 2016 at 13:13
Add a ment  | 

2 Answers 2

Reset to default 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'

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./bnoordhuis/node-iconv

or

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

Michael

发布评论

评论列表(0)

  1. 暂无评论