简体   繁体   English

如何在node.js上使用请求模块设置POST编码?

[英]How to set POST encoding with request module on node.js?

I'm using request module on node.js but there is something problem with encoding option. 我在node.js上使用请求模块,但编码选项存在问题。 beneath codes are simple post request, but I don't know how to set up encoding of form field data . 代码下面是简单的发布请求,但我不知道如何设置表单字段数据的编码 I already set headers to 'Content-Type': 'application/x-www-form-urlencoded; charset=euc-kr' 我已经将标题设置为'Content-Type': 'application/x-www-form-urlencoded; charset=euc-kr' 'Content-Type': 'application/x-www-form-urlencoded; charset=euc-kr' But it doesn't works. 'Content-Type': 'application/x-www-form-urlencoded; charset=euc-kr'但它不起作用。 field data is korean, like "안녕하세요", and I should post it with euc-kr encoding. 现场数据是韩国的,比如“안녕하세요”,我应该用euc-kr编码发布。 (The site takes euc-kr, not utf8) (该网站采用euc-kr,而不是utf8)

The same program on Java application, I coded like this : 关于Java应用程序的相同程序,我编码如下:

PrintWriter wr = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "euc-kr"));

But I don't know how to in nodejs. 但我不知道如何在nodejs。 Can anyone give some solution...? 谁能给出一些解决方案......?

Code Sample 代码示例

//Load the request module
var request = require('request');

//Lets configure and request
request({
    url: 'http://google.com', //URL to hit
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=euc-kr' },
    method: 'POST',
    form: {
        field1: 'data',
        field2: 'data'
    }
}, function(error, response, body){
    if(error) {
        console.log(error);
    } else {
        console.log(response.statusCode, body);
    }
});

Finally I got a soultion, and I solved this problem. 最后我得到了一个灵魂,我解决了这个问题。

If you send a data as a form using request module, the module change your form encoding to utf-8 by force. 如果使用请求模块将数据作为表单发送,则模块会强制将表单编码更改为utf-8。 So even you setted your form encoding to another charset, the module changes your charset to utf8 again. 因此,即使您将表单编码设置为另一个字符集,模块也会再次将您的字符集更改为utf8。 You can see that at request.js on line 1120-1130. 您可以在1120-1130行的request.js上看到。

So, You'd better send a data by 'body' option, not 'form' option. 因此,您最好通过'body'选项发送数据,而不是'form'选项。

Node doesn't support EUC-KR so you can use iconv-lite to extend the native encodings available and set the encoding option in request . Node不支持EUC-KR因此您可以使用iconv-lite扩展可用的本机编码,并在request设置encoding选项。

List of Natively Supported Encodings 本机支持的编码列表

iconv.extendNodeEncodings(); only works for node pre v4+. 仅适用于节点pre v4 +。 See here to get this working for a newer version of node. 请参阅此处以使其适用于较新版本的节点。

var iconv = require('iconv-lite');
var request = require('request');

// This will add to the native encodings available.
iconv.extendNodeEncodings();

request({
  url: 'http://google.com', //URL to hit
  method: 'POST',
  form: {
    field1: 'data',
    field2: 'data'
  },
  encoding: 'EUC-KR'
}, function(error, response, body){
  if(error) {
    console.log(error);
  } else {
    console.log(response.statusCode, body);
  }
});

iconv.undoExtendNodeEncodings();

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

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