Superagent: Parse gzipped response

62 Views Asked by At

I'm making a request that looks like the following

request
        .post(url)
        .set('authorization', `Bearer ${token}`)
        .send(data)
        .end((err, res) => {
            console.log(err, res);
        });

The response from the server is compressed however with the response header content-encoding: deflate and throws the error

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

How would I be able to parse a compressed response here?

1

There are 1 best solutions below

0
Mitch On

Superagent can not handle a response encoded with deflate (despite deflate being added to accept-encoding by default)

If I only accept gzip responses

request
        .post(url)
        .set('authorization', `Bearer ${token}`)
        .set('accept-encoding', 'gzip')
        .send(data)
        .end((err, res) => {
            console.log(err, res);
        });

Its able to decompress it, of course this only works if your endpoint supports gzip