Ivá*_*lla 8 character-encoding axios
我正在获取一个带有axios的网页,但响应的内容类型是ISO-8859-1,axios给出的结果似乎是将它解析为UTF-8,结果有损坏的字符.
我试图转换结果编码但没有任何作用,我认为它不是因为这
通过示例在获得库我可以将编码设置为null并克服该问题,但我想问你我怎么能用axios来禁用自动编码或更改它?
小智 19
您可能不需要答案,但对于其他人:
我的方法是这样的:
responseType
并responseEncoding
设置如下const response = await axios.request({
method: 'GET',
url: 'https://www.example.com',
responseType: 'arraybuffer',
responseEncoding: 'binary'
});
Run Code Online (Sandbox Code Playgroud)
reponse.data
为所需格式let html = iso88592.decode(response.data.toString('binary'));
Run Code Online (Sandbox Code Playgroud)
注意:就我而言,我需要使用此包对其进行解码。
希望能帮助到你。
小智 7
在不使用拦截器或其他包的情况下,我在缓冲区上得到了响应:
notifications = await axios.request({
method: 'GET',
url: Link,
responseType: 'arraybuffer',
reponseEncoding: 'binary'
});
Run Code Online (Sandbox Code Playgroud)
接下来将其转换为:
let html = notifications.data.toString('latin1');
Run Code Online (Sandbox Code Playgroud)
在这个 github 问题中,Matt Zabriskie 建议使用axios 响应拦截器,我认为这是最干净的选择。
axios.interceptors.response.use(response => {
let ctype = response.headers["content-type"];
if (ctype.includes("charset=ISO-8859-1")) {
response.data = iconv.decode(response.data, 'ISO-8859-1');
}
return response;
})
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4983 次 |
最近记录: |