How to handle HTTP response using Axios

Himanshu Pratap
1 min readMay 28, 2020

Once an HTTP request is made, Axios returns a promise that is either fulfilled or rejected, depending on the response from the backend service. To handle the result, you can use the then() method like this:

axios.post('/student/12', {
firstName: 'Finn',
lastName: 'Williams'
})
.then((response) => {
console.log(response);
}, (error) => {
console.log(error);
});

If the promise is fulfilled, the first argument of then() will be called; if the promise is rejected, the second argument will be called.

The fulfillment value is an response object containing the following information.

{

data: {},
// data is the response that was provided by the server
status: 200,
// status is the HTTP status code from the server response
statusText: 'OK',
// statusText is the HTTP status message from the server response
headers: {},
// headers the headers that the server responded with
// All header names are lower cased

config: {},
// config is the config that was provided to axios for request
request: {}
// request is the request that generated this response
// It is the last ClientRequest instance in node.js(in redirects)
// and an XMLHttpRequest instance the browser

}

Example:

axios.get('/student/12')
.then((response) => {
console.log(response.data);
console.log(response.status);
console.log(response.statusText);
console.log(response.headers);
console.log(response.config);
});

References:
https://blog.logrocket.com/how-to-make-http-requests-like-a-pro-with-axios/

--

--