如何从 axios 获取在 node.js 中接收 iso-8859-1 的 utf-8 值

2024-02-06

我有以下代码:

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 而不会出现这些错误?

Thanks


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

因此,至少在服务器上解决这个问题的最佳方法是首先获取一个缓冲区(axios 支持返回缓冲区吗?),将其转换为 UTF-8 字符串(唯一合法的 Javascript 字符串),然后运行JSON.parse on it.

伪代码:

// 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'));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 axios 获取在 node.js 中接收 iso-8859-1 的 utf-8 值 的相关文章

随机推荐