skip to Main Content

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.

2

Answers


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

    Login or Signup to reply.
  2. 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
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search