I created a sample to post data to a rest services and I found out that when I have non-ascii or non-latin character (please see data.firstName), my post request using TEST-REST.js will throw
error: { [Error: socket hang up] code: 'ECONNRESET' }.
// TEST-REST.js
var http = require('http');
var data = JSON.stringify({
firstName: 'Joaquìn',
});
var options = {
host: '127.0.0.1',
port: 3000,
path: '/users',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
};
var req = http.request(options, function(res) {
var result = '';
res.on('data', function(chunk) {
result += chunk;
});
res.on('end', function() {
console.log(result);
});
});
req.on('error', function(err) {
console.log(err);
});
req.write(data);
req.end();
and on my rest services, it throw me error like this:
SyntaxError: Unexpected end of input Sun Sep 08 2013 23:25:02 GMT-0700 (PDT) - at Object.parse (native)
at IncomingMessage.<anonymous> (/Volumes/Data/Program_Data/GitHub/app/node_modules/express/node_modules/connect/lib/middleware/json.js:66:27) info at IncomingMessage.EventEmitter.emit (events.js:92:17)
at _stream_readable.js:920:16 : - - - [Mon, 09 Sep 2013 06:25:02 GMT] "POST /users HTTP/1.1" 400 - "-" "-"
at process._tickDomainCallback (node.js:459:13)
if I replace firstName value from 'Joaquìn' to 'abc', everything works just fine. I think I'm missing something to support or escape to make it work.
does anyone have any idea how I solve this problem? I also tried following: require('querystring').escape(model.givenName), and it works but I'm not happy with it.
UPDATED
I found out that if I comment out: app.use(express.bodyParser());, the error disappears.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…