Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding issue with Axios

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?

like image 711
Iván Portilla Avatar asked Oct 24 '16 05:10

Iván Portilla


People also ask

How do you send a URL encoded data in Axios?

In node. js, you can use the querystring module as follows: const querystring = require('querystring'); axios. post('http://something.com/', querystring.

How do you send a body request in Axios?

Making a POST request in Axios requires two parameters: the URI of the service endpoint and an object that contains the properties you wish to send to the server. For a simple Axios POST request, the object must have a url property. If no method is provided, GET will be used as the default value.


1 Answers

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.

like image 117
Daniel Turuș Avatar answered Sep 22 '22 17:09

Daniel Turuș