gpt4 book ai didi

javascript - 如何检索 Express 上的重音字符?

转载 作者:太空宇宙 更新时间:2023-11-04 01:50:30 25 4
gpt4 key购买 nike

我的 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? � � �但不起作用..

我的 Node 版本是9.6.1问题出在正文上,其中有一个带重音的单词,这会导致 400 错误,-> 在字符 1132 处应有“,”或“}”

最佳答案

根据当前的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

关于javascript - 如何检索 Express 上的重音字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49882778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com