Last active
April 18, 2018 16:24
-
-
Save scripting/0b09137fdde631ccdad528dbf3a74705 to your computer and use it in GitHub Desktop.
Using iconv-lite to do character conversion of HTTP requests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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