How to Inspect Axios HTTP Request Object

When debugging network interactions in an application, examining the raw details of an outgoing network call can help diagnose headers, formatting issues, or connectivity failures. This guide explains how to access and inspect the underlying native request object generated by Axios—such as Node.js's ClientRequest or the browser's XMLHttpRequest—through standard responses, error handling, and interceptors.

Inspecting the Request via the Response Object

When Axios completes a successful request, it attaches the underlying native request object to the response under the .request property.

const axios = require('axios');

axios.get('https://jsonplaceholder.typicode.com/posts/1')
  .then(response => {
    // Access the underlying native request object
    const request = response.request;

    if (typeof window === 'undefined') {
      // Node.js: Inspect ClientRequest details
      console.log('Method:', request.method);
      console.log('Path:', request.path);
      console.log('Headers:', request.getHeaders());
    } else {
      // Browser: Inspect XMLHttpRequest details
      console.log('Response URL:', request.responseURL);
      console.log('Status:', request.status);
    }
  })
  .catch(error => {
    console.error(error);
  });

Inspecting the Request During Errors

If a request fails after being generated (for instance, the server never responded or a network error occurred), Axios provides the request object on the error instance via error.request.

axios.get('https://invalid-domain.example.com')
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    if (error.request) {
      // The request was made, but no response was received
      console.log('Underlying request:', error.request);
      
      // Node.js specific inspection
      if (error.request.getHeader) {
        console.log('Host header:', error.request.getHeader('host'));
      }
    } else if (error.response) {
      // The server responded with a status outside the 2xx range
      console.log('Request from response error:', error.response.request);
    } else {
      // Error setting up the request
      console.error('Setup Error:', error.message);
    }
  });

Inspecting Axios Configuration via Interceptors

To inspect the request payload, headers, and target URL before the native HTTP request is dispatched, use an Axios request interceptor. Interceptors allow you to log or modify the internal Axios config object.

// Add a request interceptor
axios.interceptors.request.use(
  config => {
    console.log('Starting Request:', {
      url: config.url,
      method: config.method,
      baseURL: config.baseURL,
      headers: config.headers,
      params: config.params,
      data: config.data
    });
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

Inspecting Raw Sockets in Node.js

If you need deeper low-level inspection in a Node.js environment, such as checking raw socket properties, TLS certificate data, or local ports, access the socket object through the ClientRequest:

axios.get('https://jsonplaceholder.typicode.com/posts/1')
  .then(response => {
    const clientRequest = response.request;
    const socket = clientRequest.socket;

    if (socket) {
      console.log('Remote Address:', socket.remoteAddress);
      console.log('Remote Port:', socket.remotePort);
      console.log('Encrypted/TLS:', socket.encrypted);
      console.log('Bytes Written:', socket.bytesWritten);
    }
  });