Home > Blockchain >  Node fetchi request using axios returns gibberish data as response
Node fetchi request using axios returns gibberish data as response

Time:12-06

I am a beginner in node and I am trying to consume an API,but response returns gibberish data.I have tested the API routes and I am sure the problem is my request but I clearly cannot tell where.I think I am making an obvious mistake but it is hard to tell. Here is the function to generate the token

const generateToken = async (req, res, next) => {
  const secret = proces.env.MPESA_CONSUMER_SECRET;
  const consumer = process.env.MPESA_CONSUMER_KEY;

  const auth = new Buffer.from(`${consumer}:${secret}`).toString("base64");
  await axios
    .get(
      "https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials",
      {
        headers: {
          authorization: `Basic ${auth}`,
        },
      }
    )
    .then((response) => {
      console.log(response);
      // token = response.data.access_token;
      next();
    })
    .catch((err) => {
      console.log(err);
      //res.status(400).json(err.message)
    });
};
app.get("/token", (req, res) => {
  generateToken();
});

the response I get after console.log is this

data: '\x1F�\b\x00\x00\x00\x00\x00\x00���R@\x02J��ɩ���%�٩yJV\n'  
    'J��.�EU�\x1E��Y�N)%IQ\x01\x05\x16���&N�f\x01J:�zS \n'  
    '2�R��3�:�M--��\n'  
    'j�,\x00�H��q\x00\x00\x00'
}

Anyhelp to even help me understand where the problem could be will be highly apprecited.

CodePudding user response:

In v1.2.1 fixed this error.

Try it after install axios(v1.2.1) again

CodePudding user response:

Downgrading to Axios version 1.1.3 worked for me

  • Related