May I be wrong about axios post based on response status
Is it possible to specifically specify the error in the .then () block in axios? For example, if the api responds with a 204 status code, can I make a mistake and run the catch block?
For example:
axios.post('link-to-my-post-service', {
json-input
}).then(response => {
if (response.status === 200) {
//proceed...
}
else {
// throw error and go to catch block
}
}).catch(error => {
//run this code always when status!==200
});
EDIT
I tried this but it didn't work:
var instance = axios.create({
validateStatus: function (status)
{
return status == 200;
}
});
axios.post('link-to-my-post-service', {input: myInput}, instance)
.then(response => {
dispatch({
type: "FETCH_SUCCESS",
payload: response.data
});
}).catch(error => {
dispatch({
type: "FETCH_FAILED",
payload: error
});
});
When I get status code 204, yet the executable block is then a () block instead of a catch block.
EDIT 2
Correct answer using Ilario's suggestion:
var instance = axios.create({
validateStatus: function (status)
{
return status == 200;
}
});
instance.post('link-to-my-post-service', {input: myInput})
.then(response => {
dispatch({
type: "FETCH_SUCCESS",
payload: response.data
});
}).catch(error => {
dispatch({
type: "FETCH_FAILED",
payload: error
});
});
Now when the status code is not 200, the blocking code is executed.
source to share
If you look at the GitHub Project Page , you will see the following option description.
/* `validateStatus` defines whether to resolve or reject the promise for a given
* HTTP response status code. If `validateStatus` returns `true` (or is set to `null`
* or `undefined`), the promise will be resolved; otherwise, the promise will be
*/ rejected.
validateStatus: function (status) {
return status >= 200 && status < 300; // default
},
So, you can create an instance with your own configuration.
var instance = axios.create({
validateStatus: function (status) {
return status == 200;
},
});
You can also set default values. They will apply to every request.
axios.defaults.validateStatus = () => {
return status == 200;
};
UPDATE 1
To customize the configuration for only a specific operation, you can replace "config" with the values ββor methods you want.
axios.post(url[, data[, config]])
UPDATE 2
I tried this but it didn't work.
You cannot pass an instance to axios.post (). You have to call the message on a new instance.
var instance = axios.create({
validateStatus: function (status) {
return status == 200;
}
});
instance.post('url', data, config);
source to share
Thanks a lot for your suggestions. The answer was simpler than I expected.
I didn't want to set any defaults to change the behavior of the axioms, so I just tried something like the code below and it worked. Each time the code is executed throw new Error("Error");
, the catch block code is executed after that.
axios.post('link-to-my-post-service', {
json-input
}).then(response => {
if (response.status === 200) {
//proceed...
}
else {
// throw error and go to catch block
throw new Error("Error");
}
}).catch(error => {
//when throw "Error" is executed it runs the catch block code
console.log(error)
});
source to share