How can I get the value in utf-8 from an axios get receiving iso-8859-1 in node.js

13,147

text/json; charset=iso-8859-1 is not a valid standard content-type. text/json is wrong and JSON must be UTF-8.

So the best way to get around this at least on the server, is to first get a buffer (does axios support returning buffers?), converting it to a UTF-8 string (the only legal Javascript string) and only then run JSON.parse on it.

Pseudo-code:

// be warned that I don't know axios, I assume this is possible but it's
// not the right syntax, i just made it up.
const notificationsBuffer = await axios.get(url, {return: 'buffer'});

// Once you have the buffer, this line _should_ be correct.
const notifications = JSON.parse(notificationBuffer.toString('ISO-8859-1'));
Share:
13,147
Admin
Author by

Admin

Updated on June 18, 2022

Comments

  • Admin
    Admin almost 2 years

    I have the following code:

    const notifications = await axios.get(url)
    const ctype = notifications.headers["content-type"];
    

    The ctype receives "text/json; charset=iso-8859-1"

    And my string is like this: "'Ol� Matheus, est� pendente.',"

    How can I decode from iso-8859-1 to utf-8 without getting those erros?

    Thanks

  • Admin
    Admin over 5 years
    Thanks for the anser!! The JSON.parse worked just fine, but I had to change from 'ISO-8859-1' to latin1 for sintaxes ajustments. axios({ method:'GET', url:url, responseType:'arraybuffer', }) .then(function (response) { console.log(response.data); console.log(JSON.parse(response.data.toString('latin1'))) });
  • Namrata Das
    Namrata Das over 5 years
    Glad I helped you get there!
  • Tom Blodget
    Tom Blodget over 5 years
    Good answer but JavaScript strings are UTF-16.
  • Namrata Das
    Namrata Das about 3 years
    @TomBlodget javascript engines might use UTF-16 under the hood, and there are some leaky abstractions... Javascript strings generally are UTF-8-ish.