The new Javascript Fetch API is really nice addition to the language and my preferable, and in fact the only bearable, way to do server requests.
The Promise based API is a lot nicer than older, purely callback-based, approaches.
The usual approach to get a text response from a server using the Fetch API looks like this:
let request = fetch(url) .then(response => response.text()) .then(handleText);
But this has one subtle problem:
I was building a client application that reads weather data from a small embedded device. We did not have direct access to changing the functionality of that device, but we could upload static pages to it, and use its existing HTML API to query the amount of registered rainfall and lightning strikes.
Using the fetch API, I quickly got the data and extracted it from the HTML but some of the identifiers had some screwed up characters that looked like decoding problems. So I checked whether the HTTP Content-Type was set correctly in the response. To my surprise it was correctly set as Content-Type: text/html; charset=iso-8859-1.
So why did my Javascript Application not get that? After some digging, it turned out that Response’s text() function always decodes the payload as utf-8. The mismatch between that and the charset explained the problem!
Obviously, I had to do the decoding myself. The solution I picked was to use the TextDecoder class. It can decode an ArrayBuffer with a given encoding. Luckily, that is easy to get from the response:
let request = fetch(url) .then(response => response.arrayBuffer()) .then(buffer => { let decoder = new TextDecoder("iso-8859-1"); let text = decoder.decode(buffer); handleText(text); });
Since I only had to support that single encoding, that worked well for me. Keep in mind that the TextDecoder is still experimental Technology. However, we had a specific browser as a target and it works there. Lucky us!
Thank you very much
Thanks, this just helped me 😉
Thanks for sharing, I’d upvote if that was StackOverflow 🙂
I’ve been battling an issue with this for months, and this just solved it for me. Thank you.
someday when u dead, u will go to heaven.
Saved my skin! Thank you so much!