Skip to content

AxiosError.response is undefined when HTTP stream is aborted after headers are received (Node.js) #6935

@theo-sim-dev

Description

@theo-sim-dev

Describe the bug

In Node.js, if the server sends headers but aborts the stream before completing the body, Axios throws an error without populating err.response, even though response metadata like status, statusText, and headers was already received. Axios currently discards this information entirely, making it inaccessible to the application. As a result, consumers cannot programmatically handle these errors based on the response metadata, which can be critical for debugging, retry logic, or user messaging.

To Reproduce

  1. Create a Node.js server that sends headers and then immediately closes the connection.
  2. Use Axios to make a request to this server.
  3. Catch the error and inspect err.response.

Code snippet

// test-server.cjs
const http = require('http');

http.createServer((req, res) => {
  console.log('[SERVER] Got request');
  res.writeHead(404, { 'Content-Type': 'text/plain' });
  res.write('Partial response');

  setTimeout(() => {
    console.log('[SERVER] Destroying connection...');
    // res.end(); // --> header is captured as expected.
    res.destroy(); // --> (Bug) If aborted, header is not captured and not accessible.
  }, 100); // 100ms delay
}).listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});
// test-client.cjs
const axios = require('axios');

axios.get('http://localhost:3000')
  .then(res => {
    console.log('Success?', res.status);
  })
  .catch((err) => {
    console.log('[CLIENT] err.message:', err.message);
    console.log('[CLIENT] err.code:', err.code);
    console.log('[CLIENT] err.response:', err.response); // Bug, it is undefined

    if (err.response) {
      console.log('[CLIENT] err.response.status:', err.response.status);
      console.log('[CLIENT] err.response.data:', err.response.data);
    }
  });

Then run both servers

node test-server.cjs
node test-client.cjs

Expected behavior

err.response should contain the response from the server in case of stream abort.

Axios Version

v1.9.0

Node.js Version

v22

Metadata

Metadata

Assignees

No one assigned

    Labels

    issue::bugThis issue is related to a bug that requires fixing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions