I am sending a status code 422 from my backend code with response body which contains the description of the error. I am using axios post as below to post a request:
post: function(url, reqBody) { const request = axios({ baseURL: config.apiUrl, url: url, headers: { 'Content-Type': 'application/json', 'Authorization': sessionStorage.getItem('token') }, method: 'POST', data: reqBody, responseType: 'json' }); return request .then((res) => { return res; }) .catch((error) => { console.log(error); return error; }) }
The problem is when backend is returning error code 422, the error object I am catching has no information about response body. Is there any way I can retrieve the error text?
axios. get('/user/12345') . catch(function (error) { if (error.
By default, the axios HTTP library throws an error anytime the destination server responds with a 4XX / 5XX error (for example, a 400 Bad Request ). Since axios raises an error, your workflow will stop at this step.
To perform an HTTP POST request in Axios, call axios. post() . Making a POST request in Axios requires two parameters: the URI of the service endpoint and an object that contains the properties you wish to send to the server. For a simple Axios POST request, the object must have a url property.
The "body" of an AXIOS error response depends from the type of response the request had. If you would like full details about this issue you can see this blogpost: How to catch the body of an error in AXIOS. In summary AXIOS will return 3 different body depending from the error:
Without a catch in your Axios call, you won't get a response unless the response status is 200. Any other response status you will only get an error in your browser console like the image below: Without a catch, you can't alert the user that there's an issue and the user doesn't know what the problem is.
If you are not receiving the expected status code, you might change the way you check the response in the interceptor. You can check any of the elements that Axios response is structured. Thanks for contributing an answer to Stack Overflow!
If we have a catch in the axios call we can send back json with the error code, and use it to inform the user that 'Authentication failed' by showing an alert. Now the user can check their inputs for errors. If we find a user in the database we can move forward to checking if the password is correct.
I had this same issue and the answer (as per Axios >= 0.13) is to specifically check error.response.data
:
axios({ ... }).then((response) => { .... }).catch((error) => { if( error.response ){ console.log(error.response.data); // => the response payload } });
See here for more details.
The "body" of an AXIOS error response depends from the type of response the request had.
If you would like full details about this issue you can see this blogpost: How to catch the body of an error in AXIOS.
In summary AXIOS will return 3 different body depending from the error:
Wrong request, we have actually done something wrong in our request (missing argument, bad format), that is has not actually been sent. When this happen, we can access the information using error.message
.
axios.get('wrongSetup') .then((response) => {}) .catch((error) => { console.log(error.message); })
Bad Network request: This happen when the server we are trying to reach does not respond at all. This can either be due to the server being down, or the URL being wrong. In this case, we can access the information of the request using error.request
.
axios.get('network error') .then((response) => {}) .catch((error) => { console.log(error.request ); });
Error status: This is the most common of the request. This can happen with any request that returns with a status that is different than 200. It can be unauthorised, not found, internal error and more. When this error happen, we are able to grasp the information of the request by accessing the parameter specified in the snippets below. For the data (as asked above) we need to access the error.response.data
.
axios.get('errorStatus') .then((response) => {}) .catch((error) => { console.log(error.response.data); console.log(error.response.status); console.log(error.response.headers); })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With