I have problems with accented characters on my POST request .. I tryed with iconv but noways.. Part of my code:
Edit: ...
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){
.....
I saw this post Module request how to properly retrieve accented characters? but doesnt work..
My node version is 9.6.1 The problem is on the body where there are a word with accent this do an 400 error , -> Expected a ',' or '}' at character 1132
According to current node.js documentation you may need the following
res.setEncoding('utf8');
So that your code looks like:
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;
});
You can see the example here: https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_http_get_options_callback
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.