MetalJr
MetalJr

Reputation: 302

Axios GET request timeouts randomly

We are working on a React app, which makes REST API calls using Axios. And we are randomly seeing request timeouts. We checked the network connection and it was fine (100 MBPS connection). We checked our server logs, the request reached the server and it got processed without any errors. But the client never got back the response. I am adding the code we use to make those api calls below. If you find any issue with the code, a solution will be much appreciated. If you think that the code is looking good, then please give us some tips on resolving/debugging the timeout. I am also adding the error below.

import axios from 'axios';
import axiosRetry from 'axios-retry';
import { v4 } from 'uuid';

axios.defaults.timeout = 30000;
axios.interceptors.request.use((config) => {
  const updatedConfig = { ...config };
  updatedConfig.headers['X-CORRELATION-ID'] = v4();
  return updatedConfig;
});
axiosRetry(axios, {
  retries: 3,
  retryDelay: (retryCount) => retryCount * 1000,
});

axios({
    method: 'get',
    url: `${requestURL}`,
    headers: {
      authorization: `Bearer ${token}`,
    },
  })
    .then((response) => {
      console.log(response);
    })
    .catch((error) => {
      console.log(error);
    });
{
    "error": {
        "message": "timeout of 30000ms exceeded",
        "name": "Error",
        "stack": "Error: timeout of 30000ms exceeded\n at e.exports (https://file.js:2:2141051)\n at XMLHttpRequest.h.ontimeout (https://file.js:2:2140157)",
        "config": {
            "url": "requestURL",
            "method": "get",
            "headers": {
                "Accept": "application/json, text/plain, */*",
                "authorization": "Bearer TOKEN",
                "X-CORRELATION-ID": "UDID"
            },
            "transformRequest": [null],
            "transformResponse": [null],
            "timeout": 30000,
            "xsrfCookieName": "XSRF-TOKEN",
            "xsrfHeaderName": "X-XSRF-TOKEN",
            "maxContentLength": -1,
            "axios-retry": {
                "retryCount": 0,
                "lastRequestTime": 1597096169071
            }
        },
        "code": "ECONNABORTED"
    }
}

Thanks in advance

Upvotes: 1

Views: 3321

Answers (0)

Related Questions