How Axios Handles Chunked Transfer Encoding

This article provides an overview of how the Axios HTTP client processes HTTP responses that use chunked transfer encoding (Transfer-Encoding: chunked). It details the default behavior of Axios across different runtime environments—Node.js and the browser—explains how data is buffered versus streamed, and demonstrates how to handle streaming data efficiently without running into memory bottlenecks.

Default Behavior: Response Buffering

By default, Axios automatically buffers chunked responses. When a server responds with Transfer-Encoding: chunked without a predefined Content-Length header, Axios listens for incoming data chunks, stitches them together in memory, and resolves the promise only after the entire response stream has finished.

For standard configurations (such as responseType: 'json' or responseType: 'text'), response.data will contain the fully assembled and parsed payload rather than individual chunks.

Behavior in Node.js

In a Node.js environment, Axios relies on the native http and https modules.

1. Default Mode (Buffered)

Axios accumulates all chunks emitted by the underlying http.IncomingMessage stream. Once the stream ends, it formats the result based on the specified responseType and resolves the request promise.

2. Streaming Mode (responseType: 'stream')

To process chunks in real time as they arrive from the server, you must set the responseType configuration to 'stream'.

const axios = require('axios');

async function downloadStream() {
  const response = await axios({
    method: 'get',
    url: 'https://example.com/large-chunked-data',
    responseType: 'stream'
  });

  // response.data is a readable stream
  response.data.on('data', (chunk) => {
    console.log(`Received chunk: ${chunk.length} bytes`);
  });

  response.data.on('end', () => {
    console.log('Stream finished.');
  });
}

Using 'stream' prevents high memory consumption when handling large payloads, such as video streams or massive datasets.

Behavior in the Browser

In the browser, Axios predominantly relies on the XMLHttpRequest (XHR) API, though newer configurations can use the Fetch API adapter.

Important Considerations