Skip to content

Instantly share code, notes, and snippets.

@arimariojesus
Last active January 25, 2024 12:15
Show Gist options
  • Save arimariojesus/11cbc068a3adae7e54604e5174c0ab58 to your computer and use it in GitHub Desktop.
Save arimariojesus/11cbc068a3adae7e54604e5174c0ab58 to your computer and use it in GitHub Desktop.
Decode a xml fetch response with ISO-8859-1 codification to UTF-8
const decodedResponse = await fetch('https://www.anysite.rss.xml', {
headers: {
'Content-Type': 'text/xml;charset=ISO-8859-1',
},
})
.then(r => r.arrayBuffer()) // resolve the response stream for buffer
.then(d => {
const dataView = new DataView(d); // creates a DataView object representing the buffer data
const isoDecoder = new TextDecoder('ISO-8859-1'); // creates an instance of the our decoder
const decodedString = isoDecoder.decode(dataView); // so we pass the stream of bytes for the decoder do its job
return new window.DOMParser().parseFromString(parsedString, 'text/xml'); // here we transform the string to html
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment