Axios的编码问题 [英] Encoding issue with Axios

查看:372
本文介绍了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:

我的方法是:


  1. 使用以下设置的 responseType responseEncoding 进行请求

  1. Make the request with responseType and responseEncoding set as below



const response = await axios.request({
  method: 'GET',
  url: 'https://www.example.com',
  responseType: 'arraybuffer',
  responseEncoding: 'binary'
});




  1. 解码响应。数据为所需格式

  1. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆