How Axios Handles UTF-8 Text Parsing

This article explains how the Axios HTTP client handles the automatic parsing of UTF-8 encoded text across different runtimes. It covers the underlying mechanisms Axios uses in both browser and Node.js environments, its default configurations for data decoding, how it handles JSON transformation, and how developers can customize encoding behavior to prevent character corruption.

Default Encoding Behavior in Axios

Axios processes incoming response data based on the execution environment (browser or Node.js) and the specified responseType. By default, Axios treats incoming data as UTF-8 encoded text when parsing JSON or plain text responses.

When a server responds with standard textual formats—such as application/json, text/plain, or text/html—Axios reads the raw byte stream and decodes it into a UTF-8 JavaScript string before performing any further transformations.

Parsing in the Browser Environment

In client-side applications, Axios relies on the browser's native XMLHttpRequest (XHR) or the fetch API.

Parsing in the Node.js Environment

In Node.js, Axios uses the native http and https modules to receive response data as streams of binary Buffer objects.

Automatic JSON Deserialization

Axios includes built-in transform functions defined in transformResponse. The process works as follows:

  1. The raw payload is received and decoded from UTF-8 bytes into a string.
  2. If the response is determined to be JSON (or if responseType: 'json' is active), Axios passes the decoded UTF-8 string to JSON.parse().
  3. The resulting JavaScript object or array is assigned to response.data.

Because string decoding happens prior to JSON parsing, any valid UTF-8 characters contained within JSON keys or values are preserved accurately.

Customizing Encoding Options

Developers can alter how Axios decodes text using request configuration options: