Is there anyone also having a problem with sending POST data with accented characters such as ä ö using nodejs http module? I have implemented an API that receives the POST data. By sending POST data with accented characters directly to API (using POSTMAN plugin), the API doesn't have problem with it. But when I submit data from nodejs using http module, I get error specifically a syntax error in json. I would be really happy if anyone can help.
var http = require('http')
var dataString = JSON.stringify(data);
var options = {
host:'dev.testapi.ev',
path:'/testapi',
method:'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': dataString.length
}
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
var responseString = '';
res.on('data', function(data) {
responseString += data;
});
res.on('end', function() {
var responseObject = JSON.parse(responseString);
success(responseObject);
});
});
req.write(dataString);
req.end();
'Content-Length': dataString.length
The length of a string is not necessarily equal to the number of bytes contained in that string. The byte length depends on the string encoding. UTF-8 uses multiple bytes to encode characters outside the 7-bit ASCII range (0 - 127). Accented characters fall outside that range.
Because dataString.length < Buffer.byteLength(dataString)
, the HTTP client is reading fewer characters than were posted, resulting in a SyntaxError.
You need Buffer.byteLength
:
var options = {
host:'dev.testapi.ev',
path:'/api/path/',
method:'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(dataString, 'utf8') //or Buffer.byteLength(dataString) as encoding defaults to utf8
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With