node.js - Restify: Retrieve external url content (ISO-8859-1) return to requesting client (UTF-8) -
i have simple javascript using node. purpose of script to:
- open listening socket
- add handler url /test http function get
- when /test requested url located on external web site should fetched. web site encoded iso-8859-1
- the data returned external website should packaged in json structure , returned requesting client encoded utf-8
so far have created following code:
var buffer = require('buffer').buffer; var iconv = require('iconv-lite'); var urllib = require('url'); var restify = require('restify'); var server = restify.createserver(); server.use(restify.bodyparser()); server.get('/test', test); server.listen(8080, function() { console.log('%s listening @ %s', server.name, server.url); }); function test(req, res, next) { console.log('test'); var httpclient = restify.createstringclient({ url: "http://dl.dropboxusercontent.com" }); httpclient.get("/u/815962/iso-8859-1.html", function(cerr, creq, cres, cdata) { cdata = iconv.decode(cdata, 'iso-8859-1'); res.send(200, {"data": cdata}); }); }
i have set test document used in code above. test document in iso-8859-1 encoding , has national letters "ÅÄÖåäö" inside it. when returned client, if read utf-8 receive "ýýýýýý"
it seem bug in restify. following example shows different results using restify , request lib:
var request = require('request'); var iconv = require('iconv'); var restify = require('restify'); var ic = new iconv.iconv('iso-8859-1', 'utf-8'); request.get({ url: 'http://dl.dropboxusercontent.com/u/815962/iso-8859-1.html', encoding: null, }, function(err, res, body) { var buf = ic.convert(body); var utf8string = buf.tostring('utf-8'); console.log(utf8string); }); var httpclient = restify.createstringclient({ url: "http://dl.dropboxusercontent.com" }); httpclient.get("/u/815962/iso-8859-1.html", function(cerr, creq, cres, cdata) { var buf = ic.convert(cdata); var utf8string = buf.tostring('utf-8'); console.log(utf8string); });
i have browsed code of restify on github trying find issue, can't
Comments
Post a Comment