I'm trying to fetch data from a public API and I have to specify the headers in the axios get request function to get a content type of application/json as and return a response.
let joke;
async function getJoke() {
try {
const res = await axios.get('https://icanhazdadjoke.com/', { headers: { 'Accept':'application/json' } });
joke = res.data;
console.log(res);
} catch (error) {
console.log(`Error Logged : Status code returned is ${error.response.status}`);
}
}
getJoke();
I've tried both, 'Content-Type' : 'application/json' and 'Accept': 'application/json' in an attempt not to fall into the error, but it's still catching the error and logging it.