我的POST请求中的重音字符有问题.. 我试着用iconv但是现在.. 我的部分代码:
修改 ...
var bodyString = JSON.stringify(req.body);
var options = {
host: XXXXX,
port: XXX,
path: url,
method: 'POST',
encoding: null,
rejectUnauthorized: false,
headers: {
"OAM_SFID": header.sfid,
"X-FORWARDED-SERVER": header.server,
"OAM_TIENE_PERFIL": header.loginType,
"OAM_REMOTE_USER": header.user,
"Content-Type": "application/json;charset=UTF-8",
'Content-Length': bodyString.length,
"Accept": "application/json"
}
};
var body = '';
var getReq = https.request(options, function (res) {
console.log("\nstatus code: ", res.statusCode); // CODE 400! -.-
var currentHeader = res.headers['content-type'];
res.setEncoding('utf8');
res.on('data', function (data) {//this data has a accented character
console.log(data);// Expected a ',' or '}' at character 1132
body += data;
});
res.on('end', function () {
var parsed = currentHeader && currentHeader.indexOf('json') != -1 ? JSON.parse(body) : body;
response.status(res.statusCode);
response.send(parsed);
if(res.statusCode === 200){
.....
我看过这篇文章Module request how to properly retrieve accented characters? � � �但是没有用..
我的节点版本是9.6.1 问题在于身体上有一个带有重音的单词,这会导致400错误, - >预期字符1132处的','或'}'
答案 0 :(得分:0)
根据当前的node.js文档,您可能需要以下
res.setEncoding('utf8');
这样你的代码就像:
var getReq = https.request(options, function (res) {
var currentHeader = res.headers['content-type'];
res.setEncoding('utf8'); // new line!
res.on('data', function (data) {
body += data;
});
您可以在此处查看示例:https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_http_get_options_callback