axios fails with unexpected end of file on a GET request
Error: unexpected end of fileShort 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.
Fix it fast
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/data2Match what you see, then jump to its fix
- 1
unexpected end of file only for compressed responses; the uncompressed request returns the full bodyThe server or proxy truncates the compressed bodyFix - 2
The 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
- 1
What does the error look like?
Error: unexpected end of fileunexpected 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/data2Fix
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.jsConfirmed by rfc-editor.org
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 2002Fix
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.jsConfirmed by nodejs.org
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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix