I'm attempting to make a request from Axios to retrieve data from GitHub. I am receiving a failure error that makes sense, and I'm wondering if this is the expected behavior and this type is simply not possible from the client side, or if there is a way to make this request that I am simply missing:
componentDidMount() {
axios
.get('https://github.com/users/lukeschlangen/contributions',{
headers: {
'Access-Control-Allow-Origin': '*'
}
})
.then(res => {
this.streakCounter(res);
})
.catch(err => console.log(err));
}
streakCounter(body) {
const $ = cheerio.load(body);
var data = [];
$('svg').find('rect').each(function(index, element) {
data.push({
count: parseInt($(element).attr('data-count')),
date: new Date($(element).attr('data-date'))
})
});
var yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
data = data.sort(function(a, b) {
return new Date(b.date) - new Date(a.date);
}).filter(function(el) {
return el.date.getTime() <= yesterday.getTime();
});
var streakCount = 0;
for (var i = 0; i < data.length; i++) {
if (data[i].count == 0) {
break;
}
streakCount++
}
console.log('streakCount:', streakCount);
}
My guess is that this is something GitHub might simply reject outright and that there is no way around this from the client side. This is the error I get in response:
Failed to load https://github.com/users/lukeschlangen/contributions: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 404.
I'd prefer to do this without a server if possible, so I want to make sure before I throw in the towel.