Axios 的编码问题 [英] Encoding issue with Axios
问题描述
我正在使用 axios 获取网页,但响应的内容类型是 ISO-8859-1,axios给出的结果好像解析为UTF-8,结果有乱码.
I am fetching a web page with axios, but the content-type of the response is ISO-8859-1, and the result given by axios seems like it parses it as UTF-8 and the result has corrupt characters.
我尝试转换结果编码但没有任何效果,我认为它不会因为 这个
I tried to convert the result encoding but nothing works, and i think it does not because this
通过 got 库中的示例,我可以将编码设置为 null 并解决问题,但我想请问你我可以用 axios 做什么来禁用自动编码或更改它?
By example in got library i can set encoding to null and overcome the problem but i would like to ask you what can i do with axios to disable the auto-encoding or change it?
推荐答案
您可能不需要答案,但对其他人来说:
You might not need the answer, but for others:
我的方法是这样的:
- 使用
responseType
和responseEncoding
设置如下请求
- Make the request with
responseType
andresponseEncoding
set as below
const response = await axios.request({
method: 'GET',
url: 'https://www.example.com',
responseType: 'arraybuffer',
responseEncoding: 'binary'
});
- 将
reponse.data
解码为所需的格式
- Decode
reponse.data
to the desired format
let html = iso88592.decode(response.data.toString('binary'));
注意:就我而言,我需要使用 this 包对其进行解码.
Note: In my case I needed to decode it using this package.
希望有帮助.
这篇关于Axios 的编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!