NodeJs post 请求 api 重音错误
NodeJs post request on a api accents error
当我 post 带有重音符号(如 éè...)的 json 服务器发回 400 错误请求错误时,我遇到了问题。我不知道如何解决这个问题。
bodyStr ='{"name":"ééé"}';
//headers
var headers = {
'Content-Type': 'application/json',
'Content-Length': bodyStr.length,
'X-Key' : this.xkey
};
//http request
request({
uri : this.httpHost + path,
method: method,
headers : headers,
body : bodyStr
},
function (error, response, body) {
if (!error && response.statusCode < 400) {
if (typeof successCallback == "function") {
successCallback(JSON.parse(body), response);
}
} else {
if (typeof errorCallback == "function") {
errorCallback(error || response.body || response.statusCode, response);
}
}
}
);
你们实施了RESTful服务吗?回复内容中是否有其他提示?
也许在 header Content-type
中设置字符集 (latin1) 可以解决问题:
Content-Type: application/json; charset=iso-8859-1
希望对你有帮助,
蒂埃里
要解决此问题,您需要使用像 iconv-lite(http://github.com/ashtuchkin/iconv-lite) 这样的模块将字符串转换为 utf-8,如下所示:
var iconv = require('iconv-lite');
bodyStr = iconv.encode(bodyStr, 'utf-8');
当我 post 带有重音符号(如 éè...)的 json 服务器发回 400 错误请求错误时,我遇到了问题。我不知道如何解决这个问题。
bodyStr ='{"name":"ééé"}';
//headers
var headers = {
'Content-Type': 'application/json',
'Content-Length': bodyStr.length,
'X-Key' : this.xkey
};
//http request
request({
uri : this.httpHost + path,
method: method,
headers : headers,
body : bodyStr
},
function (error, response, body) {
if (!error && response.statusCode < 400) {
if (typeof successCallback == "function") {
successCallback(JSON.parse(body), response);
}
} else {
if (typeof errorCallback == "function") {
errorCallback(error || response.body || response.statusCode, response);
}
}
}
);
你们实施了RESTful服务吗?回复内容中是否有其他提示?
也许在 header Content-type
中设置字符集 (latin1) 可以解决问题:
Content-Type: application/json; charset=iso-8859-1
希望对你有帮助, 蒂埃里
要解决此问题,您需要使用像 iconv-lite(http://github.com/ashtuchkin/iconv-lite) 这样的模块将字符串转换为 utf-8,如下所示:
var iconv = require('iconv-lite');
bodyStr = iconv.encode(bodyStr, 'utf-8');