Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

axios fails with unexpected end of file on a GET request

ErrorError: unexpected end of file

Short answer

Node.js throws an error when it decompresses a gzip or deflate body that ends too early. axios decompresses responses by default, so a server or proxy that cuts the body short surfaces as unexpected end of file. Request the same URL without compression to confirm, then fix the server or read the partial body on purpose.

  • Applies to axios 1, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask for the same URL with no compression. If this works while axios fails, the compressed body is being cut short.

    curl -sS -H "Accept-Encoding: identity" -o /dev/null -w "%{http_code} %{size_download}\n" https://example.com/api/data
    curl -sS --compressed -o /dev/null -w "%{http_code} %{size_download}\n" https://example.com/api/data
  2. 2Match what you see, then jump to its fix

    1. 1unexpected end of file only for compressed responses; the uncompressed request returns the full bodyThe server or proxy truncates the compressed bodyFix
    2. 2The sender is not yours to fix, and a partial body is useful (for example a large file where you only need the start)You accept an incomplete body and want what arrivedFix

What does the error look like?

  • Error: unexpected end of file
  • unexpected end of file

Cause 1 of 2

The server or proxy truncates the compressed body

1Check

Node.js zlib throws by default when it decompresses truncated data, and axios has decompress enabled by default. Compare the byte counts of the two curl calls from the first step, and check whether the response ends before its declared length.

curl -sS --compressed -D - -o /dev/null https://example.com/api/data

2Fix

Fix the sender: look at the proxy, CDN or app that compresses the response and at any timeout that closes the connection mid-body. As a client-side workaround, ask for an uncompressed body. HTTP defines identity as the no-encoding value, and a server should send an uncoded response when it is acceptable.

const axios = require('axios');

const res = await axios.get('https://example.com/api/data', {
  headers: { 'Accept-Encoding': 'identity' },
});
console.log(res.status);

3Verify

The request returns a status and complete data instead of throwing.

node client.js

Confirmed by rfc-editor.org

Did this fix it?

Cause 2 of 2

You accept an incomplete body and want what arrived

1Check

Node.js documents a finishFlush option that suppresses the truncation error, with the warning that you can no longer tell whether the input ended early.

curl -sS --compressed https://example.com/api/data | tail -c 200

2Fix

Turn off axios decompression, ask for a stream and pipe it through zlib with Z_SYNC_FLUSH. Check the result yourself, because truncation will no longer raise an error.

const axios = require('axios');
const zlib = require('node:zlib');

const res = await axios.get('https://example.com/api/data', {
  decompress: false,
  responseType: 'stream',
});
const gunzip = zlib.createGunzip({ finishFlush: zlib.constants.Z_SYNC_FLUSH });
const chunks = [];
for await (const chunk of res.data.pipe(gunzip)) chunks.push(chunk);
console.log(Buffer.concat(chunks).toString('utf8'));

3Verify

The script prints whatever part of the body arrived and exits without the error. Validate the content before you use it.

node client.js

Confirmed by nodejs.org

Did this fix it?

How do I stop it coming back?

  • Log the Content-Encoding and Content-Length of failing responses so a cut body is easy to spot.
  • Retry idempotent GET requests with a limit when a response ends early.
  • Treat a partial body as invalid data unless you chose finishFlush on purpose.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.