How to Send Raw Buffer Data Using Axios

This guide explains the standard approach to sending raw Buffer data using the Axios HTTP client in Node.js. By passing the binary buffer directly to the request payload and configuring the appropriate Content-Type header, you can reliably transfer binary files, raw byte streams, and other non-text payloads to a target server.

Sending a Buffer with Axios

In Node.js, Axios natively supports sending instances of the global Buffer class. To transmit raw binary data, pass the buffer directly as the request data payload and explicitly define the Content-Type header (typically application/octet-stream or a specific MIME type).

Basic POST Example

const axios = require('axios');
const fs = require('fs');

async function sendRawBuffer() {
  // Create a buffer directly or read from a file
  const bufferData = Buffer.from('Hello, binary world!', 'utf-8');
  // Alternatively: const bufferData = fs.readFileSync('path/to/file.bin');

  try {
    const response = await axios.post('https://example.com/api/upload', bufferData, {
      headers: {
        'Content-Type': 'application/octet-stream',
        'Content-Length': bufferData.length,
      },
    });

    console.log('Status:', response.status);
    console.log('Response:', response.data);
  } catch (error) {
    console.error('Upload failed:', error.message);
  }
}

sendRawBuffer();

Key Configuration Considerations

1. Specifying the Correct Content-Type

If the server expects generic binary data, use:

If you are uploading a specific file format as a raw buffer, use the corresponding MIME type (e.g., image/png, application/pdf).

2. Setting Content-Length

While Axios often calculates the Content-Length automatically for buffers in Node.js environments, explicitly providing Content-Length: bufferData.length prevents stream-related chunking issues and helps the receiving server allocate memory accurately.

3. Handling Large Payloads

By default, Axios limits request body sizes in Node.js. If you are uploading large buffers (over 10MB), configure maxBodyLength and maxContentLength to Infinity or a custom upper limit in bytes:

const response = await axios.post('https://example.com/api/upload', bufferData, {
  headers: {
    'Content-Type': 'application/octet-stream',
    'Content-Length': bufferData.length,
  },
  maxBodyLength: Infinity,
  maxContentLength: Infinity,
});

Using these configurations ensures your binary buffer is sent efficiently without automatic serialization or corruption.