Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node JS using http module to send POST data with accented characters such as ä , ö

Tags:

node.js

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();
like image 749
Ezra Avatar asked Feb 05 '15 12:02

Ezra


1 Answers

'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
    }
 };
like image 72
c.P.u1 Avatar answered Nov 12 '22 23:11

c.P.u1