close

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

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How can I get the status code from an http error in Axios ? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

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

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

    can I get the status code from an http error in Axios There is a new option called validateStatus in request config. You can use it to specify to not throw exceptions if status < 100 or status > 300 (default behavior). Example:
    const {status} = axios.get('foo.com', {validateStatus: () => true})

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

    can I get the status code from an http error in Axios There is a new option called validateStatus in request config. You can use it to specify to not throw exceptions if status < 100 or status > 300 (default behavior). Example:
    const {status} = axios.get('foo.com', {validateStatus: () => true})

Method 1

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);
    }
  });

Method 2

There is a new option called validateStatus in request config. You can use it to specify to not throw exceptions if status < 100 or status > 300 (default behavior). Example:

const {status} = axios.get('foo.com', {validateStatus: () => true})

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment