1

I'm using request module on node.js but there is something problem with encoding option. 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' But it doesn't works. field data is korean, like "안녕하세요", and I should post it with euc-kr encoding. (The site takes euc-kr, not utf8)

The same program on Java application, I coded like this :

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

But I don't know how to in 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);
    }
});

2 Answers 2

3

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. So even you setted your form encoding to another charset, the module changes your charset to utf8 again. You can see that at request.js on line 1120-1130.

So, You'd better send a data by 'body' option, not 'form' option.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

List of Natively Supported Encodings

iconv.extendNodeEncodings(); only works for node 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();

2 Comments

I'm on newest version of node, I don't know how can I set post encoding, Should I have to rollback my node version? If I set value val = iconv.encode("안녕","euc-kr"); But It doesn't work well,
I rollbacked nodejs version to 0.12 but it still doesn't work. How can I do it...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.