Axios Encoding Problem

I am loading the webpage using axios , but the response content type is ISO-8859-1, and the result obtained with axios seems to be parsing it as UTF-8, and the result contains corrupted characters.

I tried to convert the encoding of the result, but nothing works, and I think it is not, because it is

For example, in the resulting library, I can set the encoding to zero and overcome the problem, but I would like to ask you, what can I do with axios to disable automatic encoding or change it?

+7
source share
2 answers

You may not need an answer, but for others:

My approach was this:

  1. Make a request by installing responseTypeand responseEncodingas shown below
const response = await axios.request({
  method: 'GET',
  url: 'https://www.example.com',
  responseType: 'arraybuffer',
  responseEncoding: 'binary'
});
  1. Decode reponse.datato the desired format
let html = iso88592.decode(response.data.toString('binary'));

Note: in my case, I needed to decode it using this package.

Hope it helps.

+1
source

In this github release, Matt Zabriskey recommends using the axios response hook , which I consider the cleanest option.

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;
})
+1
source

Source: https://habr.com/ru/post/1658643/