Check Response Compression in Axios HTTP Client
This article explains how to verify and manage HTTP response compression (such as Gzip, Deflate, and Brotli) in the Axios HTTP client. You will learn how Axios handles decompression by default in Node.js and browser environments, how to inspect HTTP headers to check if a response was compressed, and how to bypass automatic decompression to perform manual decompression checks.
How Axios Handles Response Compression
By default, Axios requests compressed data by automatically appending
the Accept-Encoding: gzip, compress, deflate, br header in
Node.js environments. When the server responds with a compressed
payload, Axios automatically decompresses it before returning the final
data to your application.
In browser environments, the browser's native networking layer handles the decompression automatically before Axios receives the data.
Checking Compression via Response Headers
To check whether the server returned a compressed response, you can
inspect the content-encoding header in the Axios response
object.
const axios = require('axios');
async function checkCompression() {
try {
const response = await axios.get('https://httpbin.org/gzip');
const encoding = response.headers['content-encoding'];
if (encoding) {
console.log(`Response was compressed using: ${encoding}`);
} else {
console.log('Response was not compressed.');
}
console.log('Data:', response.data);
} catch (error) {
console.error('Request failed:', error);
}
}
checkCompression();Using Axios Interceptors for Global Verification
If you need to log or enforce compression checks across all outgoing requests, you can use an Axios response interceptor:
const axios = require('axios');
const apiClient = axios.create();
apiClient.interceptors.response.use((response) => {
const encoding = response.headers['content-encoding'];
if (!encoding) {
console.warn(`Warning: Response from ${response.config.url} was uncompressed.`);
} else {
console.log(`Verified compression (${encoding}) for: ${response.config.url}`);
}
return response;
});Disabling Automatic Decompression for Manual Checks
In Node.js, Axios uses its internal decompress: true
configuration by default. If you want to verify the raw, compressed
payload manually (for example, to test payload size or validate data
integrity before decompressing), you can set
decompress: false and set
responseType: 'arraybuffer'.
Here is how to receive the raw compressed buffer and decompress it
manually using Node.js's native zlib module:
const axios = require('axios');
const zlib = require('zlib');
async function manualDecompressionCheck() {
const response = await axios.get('https://httpbin.org/gzip', {
decompress: false, // Prevents Axios from automatically decompressing
responseType: 'arraybuffer', // Keeps raw binary data intact
});
const encoding = response.headers['content-encoding'];
const compressedSize = response.data.length;
console.log(`Compressed Payload Size: ${compressedSize} bytes`);
let decompressedData;
if (encoding === 'gzip') {
decompressedData = zlib.gunzipSync(response.data).toString('utf-8');
} else if (encoding === 'deflate') {
decompressedData = zlib.inflateSync(response.data).toString('utf-8');
} else if (encoding === 'br') {
decompressedData = zlib.brotliDecompressSync(response.data).toString('utf-8');
} else {
decompressedData = response.data.toString('utf-8');
}
console.log('Decompressed Content:', JSON.parse(decompressedData));
}
manualDecompressionCheck();Summary Checklist for Compression Verification
- Verify Request Headers: Ensure
Accept-Encodingis sent (configured automatically by Axios in Node.js). - Inspect Response Headers: Check
response.headers['content-encoding']for values likegzip,br, ordeflate. - Use Interceptors: Apply response interceptors to audit or enforce compression policies across an application.
- Control Decompression: Set
decompress: falseif you require direct access to raw compressed bytes for testing or custom decompression logic.