I'm trying to pull the html content of a given url and the origin content encoding is utf-8. I get the html of the page but the text whitin the html elemnts are returned in bad format (question marks).
This is what I do:
var parsedPath = url.parse(path);
var options = {
host: parsedPath.host,
path: parsedPath.path,
headers: {
'Accept-Charset' : 'utf-8',
}
}
http.get(options, function (res) {
var data = "";
res.on('data', function (chunk) {
data += chunk;
});
res.on("end", function () {
console.log(data);
});
}).on("error", function () {
callback(null);
});
How can I enforce the encoding of the returned data?
Thanks
Use the setEncoding()
method like this:
http.get(options, function (res) {
res.setEncoding('utf8');
var data = "";
res.on('data', function (chunk) {
data += chunk;
});
res.on("end", function () {
console.log(data);
});
});
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