How to Reproduce Failed Requests with Axios error.config

When handling errors in Axios, the error.config object contains the complete configuration used to make the original HTTP request. By examining and extracting key properties from error.config—including the URL, HTTP method, headers, request payload, and query parameters—developers can accurately inspect, log, or programmatically replay requests that encountered network failures or non-2xx status codes.

Key Properties in error.config

To reconstruct a failed request, inspect the following core properties inside error.config:

Programmatically Retrying the Request

Because error.config preserves the original request configuration, you can pass it directly back into Axios to retry the operation:

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

    // Prevent infinite retry loops
    if (error.response && error.response.status === 401 && !originalRequest._retry) {
      originalRequest._retry = true;

      // Refresh authentication token
      const newToken = await refreshAuthToken();
      originalRequest.headers['Authorization'] = `Bearer ${newToken}`;

      // Re-run the request with the original configuration
      return axios(originalRequest);
    }

    return Promise.reject(error);
  }
);

Extracting Properties to Generate a cURL Command

You can convert the properties inside error.config into a standard cURL command to reproduce the issue in a terminal or API client:

function generateCurlCommand(config) {
  const fullUrl = (config.baseURL || '') + config.url;
  const method = (config.method || 'get').toUpperCase();
  
  let curl = `curl -X ${method} "${fullUrl}"`;

  // Append headers
  if (config.headers) {
    Object.entries(config.headers).forEach(([key, value]) => {
      curl += ` -H "${key}: ${value}"`;
    });
  }

  // Append payload
  if (config.data) {
    const dataString = typeof config.data === 'object' ? JSON.stringify(config.data) : config.data;
    curl += ` -d '${dataString}'`;
  }

  return curl;
}

Using these properties ensures that the reproduction matches the exact payload, headers, and routing of the original failed call.