search

Home  >  Q&A  >  body text

Retry axios request when response is empty

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粉863295057P粉863295057257 days ago287

reply all(1)I'll reply

  • P粉556159786

    P粉5561597862024-03-20 00:57:20

    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.

    reply
    0
  • Cancelreply