'request url return errno: -3, code: 'Z_DATA_ERROR'

I have used three lib to request url return same error

URL:https://comment.bilibili.com/558679896.xml

Axios Error:

Error: incorrect header check at Zlib.zlibOnError [as onerror] (node:zlib:190:17) { errno: -3, code: 'Z_DATA_ERROR',

Got Error:

ode:internal/process/promises:246 triggerUncaughtException(err, true /* fromPromise */); ^

Error: incorrect header check at Zlib.zlibOnError [as onerror] (node:zlib:190:17) { errno: -3, code: 'Z_DATA_ERROR' }

node http:

const zlib = require('zlib');
const http = require('http');
const fs = require('fs');
const { pipeline } = require('stream');

const request = http.get({ host: 'comment.bilibili.com',
                           path: '/558679896.xml',
                           port: 80,
                           headers: { 'Accept-Encoding': 'br,gzip,deflate' } });
request.on('response', (response) => {
  const output = fs.createWriteStream('example.com_index.html');

  const onError = (err) => {
    if (err) {
      console.error('An error occurred:', err);
      process.exitCode = 1;
    }
  };

  switch (response.headers['content-encoding']) {
    case 'br':
      pipeline(response, zlib.createBrotliDecompress(), output, onError);
      break;
    // 或者,只需使用 zlib.createUnzip() 来处理以下两种情况:
    case 'gzip':
      pipeline(response, zlib.createGunzip(), output, onError);
      break;
    case 'deflate':
      pipeline(response, zlib.createUnzip(), output, onError);
      break;
    default:
      pipeline(response, output, onError);
      break;
  }
});

got:

const stream = require('stream');
const {promisify} = require('util');
const fs = require('fs');
const got = require('got');

const pipeline = promisify(stream.pipeline);
const zlib = require('zlib');


(async () => {
    await pipeline(
        got.stream('https://comment.bilibili.com/558679896.xml', {
      decompress: false,
            headers: { 'Accept-Encoding': 'deflate' }
    }).setEncoding('utf8'),
    zlib.createUnzip(),
        fs.createWriteStream('1.xml')
    );
})();


got('https://comment.bilibili.com/558679896.xml')
    .then(res => {
        console.log(res)
    })
    .catch(err => {
        console.log(err)
    })

axios:

const axios = require('axios');
const fs = require('fs');
const zlib = require('zlib');

axios('https://comment.bilibili.com/558679896.xml', {
  decompress: false,
  responseType: 'stream',
  transformResponse(data) {
    try {
      return data.pipe(zlib.createUnzip())
    } catch (e) {
      return data
    }
  }
})
  .then(res => {
    res.data.pipe(fs.createWriteStream('1.xml'))
  })

axios('https://comment.bilibili.com/558679896.xml')
  .then(res => {
    console.log(res.data)
  })
  .catch(err => {
    console.log(err)
  })


Solution 1:[1]

It is work!!!

const fs = require('fs');
const got = require('got');
const { inflateRawSync } = require('zlib');

async function main () {
    const bufferData = await got('https://comment.bilibili.com/558679896.xml', {
        decompress: false
    }).buffer();
    console.log(inflateRawSync(bufferData).toString())
}

main()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Norman Wang