Reputation: 7628
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
Upvotes: 3
Views: 6397
Reputation: 1
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
}
});
Upvotes: 0
Reputation: 7628
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...."
Upvotes: 2
Reputation: 28325
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);
}
});
}
Upvotes: 0