How to convert data to utf-8 in node.js?


I am using node.js with express. I read out data from MongoDB with Mongoose and deliver it the normal way with res.send(data). Unfortunately the delivering fails for some requests. Even so the header says the encoding is utf-8, it seems to be ANSI in some cases, causing the jsonp callback function to fail with an error.

You can reproduce the error at this page:!single/9837034 . The jsonp call fails just on some products, most of them (also the ones with special chars) work fine.

How can I ensure, that a given String is encoded in utf-8 in node.js?

4/18/2011 9:04:08 PM

Accepted Answer

Have you tried:


To ensure that your data is in utf8 and is not Buffer.

6/14/2011 2:07:40 AM

I think I got stuck in a similar issue and neebz solution worked but I had to put it in the right spot.

var req = http.request(options, function(res) {
  console.log("statusCode: ", res.statusCode);
  console.log("headers: ", res.headers);
  res.on('data', function(d) {

In the node.js docs its documented as request.setEncoding() which might be an error because it needs to be called on the res object that is created by the request.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow