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:

  • url and baseURL: The url contains the requested path or full URL. If configured, baseURL defines the root URL prefixed to url. Combining these two gives the exact target endpoint.
  • method: Specifies the HTTP verb used (e.g., 'get', 'post', 'put', 'delete'). Axios defaults to 'get' if not explicitly set.
  • data: The request payload sent in the body of POST, PUT, PATCH, or DELETE requests. This data may be a JSON string, a plain object, FormData, or a stream.
  • params: The URL query parameters appended to the request as a key-value object (or serialized using a custom paramsSerializer).
  • headers: An object containing all custom and default request headers, including Authorization, Content-Type, Accept, and user-defined tokens.
  • auth: Holds HTTP Basic Authentication credentials ({ username, password }) if basic auth was configured.
  • withCredentials: A boolean indicating whether cross-site Access-Control requests were sent with credentials like cookies or TLS client certificates.
  • timeout: The number of milliseconds before the request timed out. This helps identify if a request failed due to slow response times.

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.