Axios Payload Exceeds Server Limit Behavior
When sending data with the Axios HTTP client, exceeding payload size limits results in distinct behaviors depending on whether the limitation is enforced by the client runtime or the destination server. If the payload surpasses the client's internal threshold, Axios halts the request locally and throws an error. If the data reaches the server but exceeds server-configured constraints, the server terminates processing and typically responds with an HTTP 413 (Payload Too Large) status code or abruptly drops the connection, causing Axios to reject the request promise.
Client-Side vs. Server-Side Enforcement
Handling large payloads involves two distinct enforcement layers:
- Client-Side Restrictions (Node.js): In Node.js
environments, Axios enforces default size limits through
maxBodyLengthandmaxContentLength(default is 10 MB). If your payload exceeds this threshold, Axios throws a client-side error before the network request is fully dispatched:AxiosError: Request body larger than maxBodyLength limit - Server-Side Restrictions: In browser environments or when Node.js client limits are raised, the full payload travels to the server. If the payload size exceeds limits set by intermediate proxies (like Nginx) or backend frameworks (like Express or Spring), the server takes over error handling.
Server Behavior and Axios Responses
When a server receives a payload exceeding its configured threshold, it typically responds in one of two ways:
1. HTTP 413 (Payload Too Large)
The standard server behavior is returning an HTTP status code
413 Payload Too Large (formerly
Request Entity Too Large).
When Axios receives a 413 response:
- The promise is rejected.
- Execution jumps to the
.catch()block or a response interceptor. - The error object contains the server response:
axios.post('/upload', largeData) .catch((error) => { if (error.response && error.response.status === 413) { console.error('Payload exceeds server limit.'); } });
2. Immediate
Connection Termination (ECONNRESET)
To prevent Denial of Service (DoS) attacks and conserve bandwidth, some web servers and reverse proxies close the TCP socket immediately without waiting to read the rest of the stream or sending an HTTP response body.
When this occurs:
- Axios cannot retrieve an HTTP status code.
- The error returned is a network error such as
ERR_BAD_RESPONSE,ECONNRESET, orsocket hang up. error.responsewill beundefined, anderror.requestwill contain the unfulfilled request details.
How to Resolve Payload Size Errors
Resolving payload size issues requires adjusting configurations on both the client and the server.
Configure Axios
To prevent Axios from blocking large uploads in Node.js, set
maxBodyLength and maxContentLength to
Infinity:
const axios = require('axios');
axios.post('https://example.com/api/upload', largeData, {
maxContentLength: Infinity,
maxBodyLength: Infinity
});Configure the Server and Reverse Proxy
Ensure that all intermediary services are configured to accept the larger payload size:
- Express.js (body-parser):
app.use(express.json({ limit: '50mb' })); app.use(express.urlencoded({ limit: '50mb', extended: true })); - Nginx:
client_max_body_size 50M;