简体   繁体   English

Node.js:在win1251字符集中发送http请求

[英]Node.js: Send http request in win1251 charset

How can I send the following query in win1251 charset? 如何在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();

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 更新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? 服务器是否真的在URL的查询部分接受win1251?

What character set should I assume the encoded characters in a URL to be in? 我应该假设URL中的编码字符是什么字符集?

But here are some SO answers that match your question: 但是这里有一些与您的问题相符的SO答案:

Converting from Windows-1251 to UTF-8 in Node.js 在Node.js中从Windows-1251转换为UTF-8

nodejs http response encoding nodejs http响应编码

Which reduce down to using either of these libraries, which you should also find on npm. 这减少了使用这些库中的任何一个,你也应该在npm上找到它们。

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

or 要么

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

Michael 迈克尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM