我想通过请求模块使用 euc-kr 字符集将发布表单数据发送到某个网站。我也使用 iconv-lite 模块,因为 Nodejs 支持的字符集不够。
无论如何,该网站使用euc-kr
字符集,所以我必须处理表单数据的编码(Node的默认字符集是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
最佳答案
终于灵机一动,解决了这个问题。
如果您使用请求模块以表单形式发送数据,该模块会强制将您的表单编码更改为 utf-8。因此,即使您将表单编码设置为另一个字符集,该模块也会再次将您的字符集更改为 utf8。您可以在 request.js 的第 1120-1130 行看到这一点。
所以,您最好通过“body”选项发送数据,而不是“form”选项。 (作为查询字符串)
body: "someKey=someValue&anotherKey=anotherValue...."
关于node.js - 在nodejs上的post请求中设置字符集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34240975/