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'
});
- 解码
响应。数据
为所需格式
- Decode
reponse.data
to the desired format
let html = iso88592.decode(response.data.toString('binary'));
注意:在我的情况下,我需要使用此程序包。
Note: In my case I needed to decode it using this package.
希望它会有所帮助。
这篇关于Axios的编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!