node.js - Nodejs HTTP request fails with some character -


i saving data using http request , working fine. following code:

var data = {     title: request.body.title,     description: request.body.description };  var datastring = json.stringify(data); logger.debug(datastring);  var options = {     hostname: config.hostname,     port: config.defaultport,     path: '/api/save',     method: 'put',     headers: {         'content-type': 'application/json',         'content-length': datastring.length,         'authtoken': token     } };  var req = http.request(options, function (res) {     var datastr = '';      res.setencoding('utf8');     res.on('data', function (chunk) {         datastr += chunk;     });     res.on('end', function () {         var res_data = json.parse(datastr);         if (res_data.status === "success") {             response.note = json.parse(datastr);             callback(null, response);         } else if(res_data.status === "failure") {             callback(res_data.message, response);         }     }); }); req.write(datastring); req.end(); req.on('error', function (e) {     logger.error('problem request: ' + e.message);     //callback('problem request: ' + e.message, null, null); }); 

but if title or description having character giving me error socket hang up , request fails. why character causing problem.

working solution: have using encodeuricomponent() before sending request , decodeuricomponent() after receiving request data, working. problem solution every sending , receiving request data have encode , decode.

is there better solution of situation?


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -