3

I want to send post form data to some website using euc-kr charset by request module. And I use iconv-lite module too because nodejs supported charset isn't plenty.

Anyway, The website use euc-kr charset, So I have to handle form data's encoding(Node's default charset is utf-8). But it dosen't work well, I tried to change some options so many times but I stucked untill now, So could you tell me some hints.?

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


        }
    });

}

And here is result, odd characters.

I tried :

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

result is ????@?@@?

3 Answers 3

2

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. (as a querystring)

body: "someKey=someValue&anotherKey=anotherValue...."
Sign up to request clarification or add additional context in comments.

Comments

0

Notice how it handles receiving back the body encoding

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

1 Comment

Hi.thanks for your answer but I want to handle request form datas not receiving datas...
0

After reading the source code several hours, finally I found the simple solution.

First, write your encoding function, which input a string and output a encoded string:

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

Here is a Chinese encoder based on urlencode

Then just add a qsStringifyOptions option while posting:

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

Comments

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.