New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
response status or response statusText is not available for 4xx/5xx series Http Status #1427
Comments
What is your code that is non working? const axios = require('axios');
axios.get('/foo').then(null, error => console.log(error.response.status)); // 404 |
Here is the sample code I am trying out to handle 4xx, 5xx, and 302 response code. import axios from 'axios';
class XHRService {
getWithParams(config, filter, request, receivedSuccess, receivedFailure) {
return (dispatch, getState) => {
if (request) {
dispatch(request(filter));
}
const reqConfig = this.prepareConfig('get', config);
// adding interceptor to find out response or error object structure.
axios.interceptors.response.use(function (response) {
console.log('inside axios response interceptor.');
console.log('JSON.stringify(response) ---> ' + JSON.stringify(response));
return response;
}, function (error) {
console.log('inside axios response interceptor catch block.');
console.log('JSON.stringify(error) ---> ' + JSON.stringify(error));
return Promise.reject(error);
});
return axios(reqConfig).then(response => {
if (receivedSuccess) {
dispatch(receivedSuccess(filter, response.data));
}
}).catch(function (error) {
console.log('inside catch block.');
if (error.response) {
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
console.log(error.request);
} else {
console.log('Error', error.message);
}
console.log(JSON.stringify(error));
});
}
}
// code related to prepareConfig(....)
// some shared code.
} Let me know your thoughts on the same. |
Ok. So I found out that
|
I'm also experiencing this issue. On certain 4XX errors, my API contains a payload in the body with further information on what went wrong. When my 400 response has a body, If you check out section 6.5 of RFC 7231 you'll see that for several codes, the server should be providing response bodies in many situations (403, 406). At the moment, I'm unable to access the response code in the catch which is creating issues. |
I'm also have this issue. I like to show some information about the error reason to the user. But the statusText property is always undefined. (react-native 0.55.4 with axios 0.18.0) |
I tried to reproduce this in a basic unit test by adding the following to test/unit/adapters/http.js:
The test passed and the expected status code was available on I also added the following to test/specs/requests.spec.js:
This also passed, so |
While watching the Jasmine tests running and seeing that the node unit tests pass, I thought It would be prudent test my web application in different browsers. Turns out that in Chrome, the code runs as expected - Using Safari, |
Same issue -- backend sends a HTTP 400 with a response body. Axios code does not catch() it as an error, and instead returns a null response object. So I cannot handle different HTTP 400 cases and respond to the user with more nuanced feedback on what they did wrong Code -
Context |
I had the same issue. I found that if there is an error either from the server or in the .then function axios will return an error object and the response must be accessed on err.response.data
|
@tebs1200 yeah, we're having the same problem with safari. chrome works for us. |
This works for me -- you get e.g. 403 as a valid response. Then just filter by status code in your then() handler. |
This looks like a design decision of @axios team might be worth revisiting. Probably by now this issue is ready to be closed. @emilyemorehouse |
It works, ☺ |
same issue here, when i recve 404 or 403 error, the statusText is undefined ( server response with some text explaining the error) |
For me something weird is happening. When request succeeds I get |
As far as I know, users can solve it by setting their own Closing this issue first, because it mixes lots of people problems, but feel free to open a new one if it is still there. |
@brasrox any update on this? StatusText was still undefined �although i have passed it through api request. Still stuck at the moment! So sad! |
Hello All,
We are working on react-redux client application where axios library used for any XHR request. Sometimes backend service (Spring Boot REST endpoint) returns 4xx/5xx series HTTP response code. While we are trying to catch response status code from error object, found it is not available. Is there anyway HTTP response code can be extracted during error scenarios. We are using axios version 0.15.2.
Look forward to hear back from community.
Thanks
Context
The text was updated successfully, but these errors were encountered: