How to get response status code in axios? Standard solutions doesn't work

Viewed 1092

I have this axios code and I can't reach response status code. What is wrong here? I get 'undefined' instead of 201, for example.

Thanks in advance!

  axios.post('endpoint', { body })
    .then((response) => {
        console.log(response.status);
    })
2 Answers

In case of error you have to catch the result

axios.post('endpoint', { body })
    .then((response) => {
        // do something
    })
    .catch((error) => {
        console.log(error.response.status)
    })

    post(endpoint, body) {
        return axios.post(endpoint, body)
          .then((response) => {
            return { 
               error: null, 
               data: response.data, 
               status: response.status 
             };
          })
          .catch((error) => {
            return { error: error };
      });
  }
Related