21

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 of this

For example in the 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?

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Iván Portilla
  • 461
  • 1
  • 3
  • 13

4 Answers4

32

My approach was this:

  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. 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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Daniel Turuș
  • 596
  • 6
  • 19
  • It works very well also if you have to fetch a png and save to disk. You can pass `response.data` to `fs.writeFile()` after setting `responseType` and `responseEncoding` as suggested by @Daniel – Jacopo Sep 14 '19 at 09:31
  • hi, I really thank your help, I solved just for what you write. I am pretty new coding, could you reply these questions? - I didn't use decode, just => $('.aditem', result.data.toString('binary')).each((i, element) => { - last posted here, said lating1 too, what's the difference? - could you send where to start learning about this? your resource, please thanks a lot – titoih May 22 '20 at 15:03
  • Which type is `iso88592`? I get `Cannot find name 'iso88592'.ts(2304)`. You may need `import * as iso88592 from 'iso-8859-2'` from `iso-8859-2` npm package. I see your link at the end, please put it on top of your answer. – Timo Nov 29 '22 at 12:16
  • iso88592 is undefined and I have no idea what its meant to be :D – Jamie Hutber Mar 02 '23 at 14:12
13

Without using interceptor or another package, I got the response on a buffer with:

notifications = await axios.request({
    method: 'GET',
    url: Link,
    responseType: 'arraybuffer',
    reponseEncoding: 'binary'
});

And next converting it with:

let html = notifications.data.toString('latin1');
ThreeCheeseHigh
  • 1,429
  • 5
  • 22
  • 40
4

In this github issue Matt Zabriskie recommends using an axios response interceptor, which I think is 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;
})
ThreeCheeseHigh
  • 1,429
  • 5
  • 22
  • 40
Guido
  • 46,642
  • 28
  • 120
  • 174
3

const notifications = await axios.request({
    method: 'GET',
    url: 'https://...your link',
    responseType: 'arraybuffer',
    reponseEncoding: 'binary'
});

const decoder = new TextDecoder('ISO-8859-1');
let html = decoder.decode(notifications.data)