I am trying to access free API data using AXIOS module in NodeJS. Below is the code sample of the same and it is giving HTTP 401 status code - unauthorized
error.
Code Sample :
const axios = require('axios');
const axiosCookieJarSupport = require('axios-cookiejar-support').default;
axiosCookieJarSupport(axios);
const URL = `https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY`;
const getData = async (url) => {
console.log(`Get Data : `);
try {
const response = await axios.get(url, {withCredentials: true});
console.log(response);
} catch (error) {
console.error(error);
}
}
getData(URL);
I did try to use tough-cookie
npm module but ending up with same error everytime.
const axios = require('axios');
const axiosCookieJarSupport = require('axios-cookiejar-support').default;
axiosCookieJarSupport(axios);
const tough = require('tough-cookie');
const cookieJar = new tough.CookieJar();
const URL = `https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY`;
const getData = async (url) => {
console.log(`Get Data : `);
try {
const response = await axios.get(url, {withCredentials: true});
console.log(response);
} catch (error) {
console.error(error);
}
}
getData(URL);
const fetchData = async (url) => {
const response = await axios.get(url, {
headers: {
'accept': '*/*',
'User-Agent': 'Mozilla/5.0'
},
jar: cookieJar,
withCredentials: true // If true, send cookie stored in jar
});
console.log(response);
}
fetchData(URL);
Any help would be much appreciated, thanks in advance.
question from:
https://stackoverflow.com/questions/66062257/unauthorized-error-when-trying-to-access-an-api-using-axios-in-nodejs 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…