如何从 axios 获取 utf-8 中的值 get receiving iso-8859-1 in node.js

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

我有以下代码:

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

ctype接收"text/json; charset=iso-8859-1"

而我的字符串是这样的:“'Ol� Matheus, est� pendente.',”

如何从 iso-8859-1 解码为 utf-8 而不会出现这些错误?

谢谢

text/json; charset=iso-8859-1 不是有效的标准内容类型。 text/json 错误,JSON 必须是 UTF-8。

所以至少在服务器上解决这个问题的最好方法是首先获取一个缓冲区(axios 是否支持返回缓冲区?),将其转换为 UTF-8 字符串(唯一合法的 Javascript 字符串)然后才 运行 JSON.parse 就可以了。

伪代码:

// 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'));