Retry axios request when response is empty
P粉863295057
P粉863295057 2024-03-19 17:20:54
0
1
311

await axios
    .post(
      executeBatch,
      {
        headers: {
          "Content-Type": "application/json",
          "x-access-token": localStorage.getItem("token"),
        },
      }
    )
    .then(
      (response) =>
        this.$store.commit(
          "insertOutputFile",
          response.data.outputFile._id
        ),
    );

  alert("You can download the result");

So sometimes I get an empty response with status code 200, Considering retrying the request if this happens, I want to know what is the correct way to solve this problem.

P粉863295057
P粉863295057

reply all(1)
P粉556159786

I think axios interceptor is suitable for you.

axios.interceptors.response.use((response) => {
    return response
  },
  async function(error) {
    const originalRequest = error.config;

    if () { // condition
      originalRequest._retry = true;
    }
  }
)

You can create the setupAxios file in the base redux directory and export it from index.js in the base redux directory.

export {default as setupAxios} from "./setupAxios";

And define setupAxios

from the root index.js file
import * as _redux from "./redux";

_redux.setupAxios(axios, store);

By the way, I'm using react.js, it might be a little different in vue.js.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template