【问题标题】:Setting charset in post request on nodejs在nodejs上的post请求中设置字符集
【发布时间】:2018-01-30 00:02:23
【问题描述】:

我想通过请求模块使用 euc-kr 字符集将表单数据发送到某个网站。我也使用 iconv-lite 模块,因为 nodejs 支持的字符集并不多。

无论如何,网站使用euc-kr 字符集,所以我必须处理表单数据的编码(节点的默认字符集是utf-8)。但是效果不太好,我尝试了很多次更改一些选项,但我一直坚持到现在,所以你能告诉我一些提示吗?

// added module request, iconv-lite(extendNodeEncoding) already.

function postDocumentForm() {
//Lets configure and request
    request({
        url: 'http://finance.naver.com/item/board_act.nhn', //URL to hit
        headers: { 
            'Content-Type': 'content=text/html; charset=euc-kr'
        },
        method: 'POST',
        encoding: 'euc-kr',
        form: {
            code:'000215',
            mode: 'write',
            temp: '',
            keyCount: '0',
            title: "폼 데이터 중 일부가 한글일 때",
            opinion: '0',
            body:'인코딩이 제대로 되지 않고 있음!'
        }
    }, function (error, response, body) {
        if (error) {
            console.log(error);
        } else {
            iconv.undoExtendNodeEncodings();
            console.log(response.statusCode, response.body);


        }
    });

}

这是结果,奇怪的字符。

我试过了:

euc-kr to binary 
euc-kr to null 
euc-kr to utf-8
delete encoding option 
delete request header 

【问题讨论】:

    标签: node.js character-encoding request


    【解决方案1】:

    最后我得到了一个灵魂,我解决了这个问题。

    如果您使用请求模块将数据作为表单发送,该模块会强制将您的表单编码更改为 utf-8。因此,即使您将表单编码设置为另一个字符集,该模块也会再次将您的字符集更改为 utf8。您可以在第 1120-1130 行的 request.js 中看到这一点。

    所以,您最好通过“body”选项发送数据,而不是“form”选项。 (作为查询字符串)

    body: "someKey=someValue&anotherKey=anotherValue...."
    

    【讨论】:

      【解决方案2】:

      注意它如何处理接收回正文编码

      iconv  = require('iconv-lite');
      
      function postDocumentForm() {
      //Lets configure and request
          request({
              url: 'http://finance.naver.com/item/board_act.nhn', //URL to hit
              headers: { 
                  'Content-Type': 'content=text/html; charset=euc-kr'
              },
              method: 'POST',
              encoding: null,
              form: {
                  code:'000215',
                  mode: 'write',
                  temp: '',
                  keyCount: '0',
                  title: "폼 데이터 중 일부가 한글일 때",
                  opinion: '0',
                  body:'인코딩이 제대로 되지 않고 있음!'
              }
          }, function (error, response, body) {
              if (error) {
                  console.log(error);
              } else {
                  console.log(response.statusCode);
                  var utf8String = iconv.decode(new Buffer(body), "ISO-8859-1");
                  console.log(utf8String);
              }
          });
      }
      

      【讨论】:

      • 嗨。谢谢您的回答,但我想处理未接收数据的请求表单数据...
      【解决方案3】:

      看了几个小时的源码,终于找到了简单的解决办法。

      首先,编写你的编码函数,输入一个字符串,输出一个编码字符串:

      const urlencode = require('urlencode');
      function encoder(s){
          return urlencode(s, 'gb2312');
      }
      

      这是一个基于urlencode的中文编码器

      然后在发帖时添加qsStringifyOptions 选项:

      request({
          url: 'http://finance.naver.com/item/board_act.nhn', //URL to hit
          headers: { 
              'Content-Type': 'content=text/html; charset=euc-kr'
          },
          method: 'POST',
          encoding: null,
          form: {
              code:'000215',
              mode: 'write',
              temp: '',
              keyCount: '0',
              title: "폼 데이터 중 일부가 한글일 때",
              opinion: '0',
              body:'인코딩이 제대로 되지 않고 있음!'
          },
          qsStringifyOptions: {
              encoder: encoder
          }
      });
      

      【讨论】:

        猜你喜欢
        • 2012-01-05
        • 1970-01-01
        • 1970-01-01
        • 2017-01-19
        • 1970-01-01
        • 2020-12-22
        • 2018-06-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多