Axios interceptors with Node.js http and browser XHR/fetch adapters: binary data transformation consistency?
0 reputation · 14 Jul 2025, 15:19 UTC
0 reputation · 14 Jul 2025, 15:19 UTC
The goal is to clarify whether axios request and response interceptors receive binary payloads in a uniform representation regardless of the underlying adapter, or if they expose adapter‑specific types such as Node.js Buffer versus browser ArrayBuffer.
The axios documentation states that interceptors operate on the config object but does not guarantee that the data property is normalized across adapters. This leaves uncertainty about how binary data is presented to interceptors when using the default Node.js http adapter compared to the XHR or Fetch adapters in browsers.
Does a request interceptor see the request body as a Buffer in Node.js and as an ArrayBuffer in the browser when the original payload is a binary blob?
Does a response interceptor receive response.data as a Buffer or ArrayBuffer depending on the adapter, or does axios convert it to a common type?
Are there any configuration options to enforce a consistent binary data type across adapters within interceptors?
35275 reputation · 14 Jul 2025, 16:06 UTC
Axios interceptors do not provide a uniform representation of binary data across different adapters. The type of the payload received in an interceptor depends entirely on the environment and the adapter being used.
In a request interceptor, config.data remains the original type passed to the Axios request method. Axios does not perform automatic normalization of binary types before they hit the interceptor:
Buffer, the interceptor sees a Buffer.ArrayBuffer or Blob, the interceptor sees that specific type.Response interceptors receive response.data as delivered by the underlying adapter. Because the Node.js http adapter and browser XHR/Fetch adapters utilize different native binary handles, the types diverge:
| Environment | Adapter | Typical Binary Type in Interceptor |
|---|---|---|
| Node.js | http | Buffer |
| Browser | XHR / Fetch | ArrayBuffer (if responseType: 'arraybuffer') |
| Browser | XHR / Fetch | Blob (if responseType: 'blob') |
There is no built-in Axios configuration option to force a single binary type across all adapters. To achieve consistency, you must implement a manual transformation. The most reliable common denominator is the Uint8Array, as it is supported in both modern browsers and Node.js.
To normalize response data, use a response interceptor or the transformResponse array:
axios.interceptors.response.use((response) => {
if (response.data instanceof Buffer) {
// Convert Node.js Buffer to Uint8Array
response.data = new Uint8Array(response.data);
} else if (response.data instanceof ArrayBuffer) {
// Convert Browser ArrayBuffer to Uint8Array
response.data = new Uint8Array(response.data);
}
return response;
});
To verify the types in your specific version of Axios (assumed 1.x), use the following checks within your interceptors:
Buffer.isBuffer(response.data).response.data.constructor.name to distinguish between ArrayBuffer and Blob.Diagnostic Detail Required: If you are using a custom adapter or a specific polyfill for Buffer in the browser, please specify, as this may lead to Buffer types appearing in browser interceptors.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.