Reputation: 69
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.
Upvotes: 1
Views: 317
Reputation: 11
If you are using an older version of axios v1.2.1 should fix the issue
Upvotes: 1
Reputation: 9310
Try it after install axios(v1.2.1) again
Upvotes: 1