How can I get the status code from an HTTP error in Axios?
Full error message
I'm trying to get the error data when a request fails in Axios.
axios
.get('foo.example')
.then((response) => {})
.catch((error) => {
console.log(error); //Logs a string: Error: Request failed with status code 404
});
Instead of the string, is it possible to get an object with perhaps the status code and content? For example:
Object = {status: 404, reason: 'Not found', body: '404 Not found'}Solutionsource: stackoverflow \u2197
What you see is the string returned by the toString method of the error object. (error is not a string.) If a response has been received from the server, the error object will contain the response property: axios.get('/foo') .catch(function (error) { if (error.response) { console.log(error.response.data); console.log(error.response.status); console.log(error.response.headers); } });
API access
Get this solution programmatically \u2014 free, no authentication.
curl https://depscope.dev/api/error/632cd9373b95b5608ff94e910b31031e477b8ba0a02adb8bc2726f57609106behash \u00b7 632cd9373b95b5608ff94e910b31031e477b8ba0a02adb8bc2726f57609106be