Axios Content-Type for Binary Data and ArrayBuffers
When sending binary data or ArrayBuffer instances using
Axios, the library determines the HTTP Content-Type header
through automatic data-type inspection in its request transformers.
Because raw memory buffers lack inherent metadata, Axios identifies the
underlying object type at runtime and either falls back to standard
binary MIME types like application/octet-stream or relies
on explicit user configuration.
Axios Runtime Type Checking
Axios processes outgoing payloads through its default request
transformers (transformRequest). During this phase, Axios
uses internal utility functions to detect the exact format of the
data payload:
ArrayBufferandArrayBufferView: Axios checks if the object is anArrayBufferusingArrayBuffer.isView()orinstanceof ArrayBuffer.- Node.js
Buffer: In Node.js environments, it checks for nativeBufferinstances viaBuffer.isBuffer(). Blob: In browser environments, Axios checks if the object is an instance ofBlob.FormData: Axios inspects if the payload is aFormDatainstance.
Automatic Header Assignment
- ArrayBuffer, TypedArrays, and Node Buffers: When
raw binary formats such as
ArrayBuffer,Uint8Array, or Node.jsBufferare supplied without a predefined header, Axios cannot infer the specific file format (e.g., JPEG, PDF, ZIP) because raw bytes contain no MIME metadata. Axios automatically sets theContent-Typeheader toapplication/octet-streamor leaves the transport adapter to handle raw byte streaming. - Blob Objects: Unlike
ArrayBuffer, aBlobcontains a nativetypeproperty. If you pass aBlobin the browser and have not manually defined a header, Axios utilizes theBlob.typevalue to set theContent-Type(for example,image/png). If theBlob.typeis empty, it falls back toapplication/octet-stream. - FormData: When sending binary data inside a
FormDatacontainer (multipart upload), Axios removes any staticContent-Typeheader to let the browser or adapter automatically generatemultipart/form-dataalong with the appropriate boundary string.
Overriding the Content-Type Manually
Because an ArrayBuffer defaults to
application/octet-stream, you must explicitly declare the
Content-Type in the request configuration if the receiving
server expects a specific MIME type:
import axios from 'axios';
const buffer = new ArrayBuffer(8);
axios.post('https://api.example.com/upload', buffer, {
headers: {
'Content-Type': 'application/pdf'
}
});When an explicit Content-Type is provided in the
configuration object, Axios respects the user definition and overrides
its automatic type deduction.