Encoding conversion of a fetch response

后端 未结 2 1162
旧巷少年郎
旧巷少年郎 2020-12-17 03:03

Inside a React Native method I\'m fetching a xml encoded in ISO-8859-1.

As long as the fetching is completed I\'m trying to convert it to UTF-8.

Here the cod

2条回答
  •  独厮守ぢ
    2020-12-17 03:57

    The best workaround is to use res.arrayBuffer() instead res.text(), as long the Buffer constructor accepts ArrayBuffer

    The code:

    fetch('http://www.band.uol.com.br/rss/colunista_64.xml')
          .then(res => res.arrayBuffer())
          .then(arrayBuffer => iconv.decode(new Buffer(arrayBuffer), 'iso-8859-1').toString())
          .then(converted => console.log(converted))
    

提交回复
热议问题