Sebastian Olsen Sebastian Olsen -4 years ago 846
Javascript Question

How can I get the status code from an http error in Axios?

This may seem stupid, but I'm trying to get the error data when a request fails in Axios.

.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'}

Answer Source

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:

  .catch(function (error) {
    if (error.response) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download