Home > OS >  Axios in Ubuntu returns a response in unreadable form, in Windows same snippet works well
Axios in Ubuntu returns a response in unreadable form, in Windows same snippet works well

Time:11-29

I have a simple code that fetches data from url. I have executed same code in both Ubuntu(server-only) and Windows

Code

import axios from "axios";
(async () => {
    const url = "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/inr/usd.json";
    fetch(url).then((res) => {
        return res.json();
    }).then((data) => {
        console.log("fetch", data);
    });

    axios.get(url).then((res) => {
        console.log("axios", res.data);
    });
})();

OUTPUT:

In windows: fetch { date: '2022-11-25', usd: 0.012218 } axios { date: '2022-11-25', usd: 0.012218 }

In Ubuntu: axios 0D��_�XV��ؚ�!�I,�8�j��8��K9"�2�wOvx�� fetch { date: '2022-11-25', usd: 0.012218 }

It worked well before, but now its coming like this. Response codes are 200 in both the cases.

I have tried updating axios and ubuntu but nothing worked.

CodePudding user response:

This works:

(async () => {
const url = "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/inr/usd.json";
fetch(url)
.then(res => res.json())
.then(data => console.log(data))
})();

Your code is messed up, please double check it

CodePudding user response:

I think this is an error in Axios library. I found 2 solutions for this.

  1. You can downgrade to version 1.1.2 (This didn't work for me.)

  2. You can change Accept-Encoding as a workaround

for the 2nd solution, change ur code as follows, it should work.

axios.get(url, { headers: { Accept: 'application/json', 'Accept-Encoding': 'identity' }, params: { trophies: true } })
.then((res) => {
    console.log("axios", res.data);
});

discussion about this problem on GitHub

  1. #5298
  2. #5296
  • Related