Skip to content

Instantly share code, notes, and snippets.

@scripting
Last active April 18, 2018 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scripting/0b09137fdde631ccdad528dbf3a74705 to your computer and use it in GitHub Desktop.
Save scripting/0b09137fdde631ccdad528dbf3a74705 to your computer and use it in GitHub Desktop.
Using iconv-lite to do character conversion of HTTP requests
const iconv = require ("iconv-lite");
const request = require ("request");
const utils = require ("daveutils");
var feedUrl = "https://www.presseportal.de/rss/dienststelle_110972.rss2";
function getCharset (httpResponse) {
var contentType = httpResponse.headers ["content-type"];
if (contentType !== undefined) {
var encoding = utils.trimWhitespace (utils.stringNthField (contentType, ";", 2));
if (encoding.length > 0) {
var charset = utils.trimWhitespace (utils.stringNthField (encoding, "=", 2));
console.log ("getCharset: charset == " + charset);
return (charset);
}
}
return (undefined); //no charset specified
}
var options = {
url: feedUrl,
encoding: null
};
request (options, function (err, response, theBuffer) {
if (err) {
console.log (err.message);
}
else {
if (response.statusCode != 200) {
console.log (response.statusCode);
}
else {
var theCharset = getCharset (response);
console.log (theCharset);
var s = iconv.decode (theBuffer, theCharset);
console.log (s);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment