I use this program to get the json data from https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY but since this morning it's not working as it returns <Response [401]>. The link loads fine on chrome though. Is there any way to fix this without using axios?
Asked
Active
Viewed 827 times
1
-
https://github.com/maanavshah/stock-market-india check this – Ramesh KR Jul 17 '21 at 17:10
-
Thank you @RameshKR for this reference. I am thankful to you. – Raj Shrishrimal Jul 19 '21 at 04:40
1 Answers
0
I tried same thing with node js please use node js ide link here for online ide please review my code
const axios = require('axios');
let cookie;
let url_oc = "https://www.nseindia.com/option-chain"
let url = "https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY"
let headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
'accept-language': 'en,gu;q=0.9,hi;q=0.8', 'accept-encoding': 'gzip, deflate, br'
}
const instance = axios.create({
baseURL: url_oc,
headers: headers,
cookie: cookie ? cookie : ""
});
const getCookies = async () => {
try {
const response = await instance.get(url_oc);
cookie = response.headers['set-cookie'].join();
} catch (error) {
if (error.response.status === 403) {
console.log("getCookies =========> error.status === 403");
await getCookies()
} else {
console.log("getCookies =========> error");
}
}
}
const getAPIData = async () => {
try {
if (cookie) {
const response = await instance.get(url);
console.log(response.data.records.timestamp);
}
} catch (error) {
if (error.response && error.response.status === 401) {
console.log("getAPIData =========> error.status === 401");
if (!cookie) {
console.log("getAPIData =========> cookie not found");
await getCookies()
}
await getAPIData()
} else {
console.log("getAPIData =========> error");
}
}
}
(async () => {
setInterval(async () => {
await getCookies()
}, 5000);
setInterval(async () => {
await getAPIData()
}, 3000);
})()

Raj Shrishrimal
- 21
- 3