Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Brotli support #1811

Closed
yigaldviri opened this issue Jul 14, 2024 · 1 comment
Closed

[fix] Brotli support #1811

yigaldviri opened this issue Jul 14, 2024 · 1 comment
Labels

Comments

@yigaldviri
Copy link
Contributor

Describe the bug

Node.js version: Node 14, 16, 18, 20

OS version: Mac Ventura 3.0.1

Description: It seems like its not possible to receive a response body when the content is compressed using Brotli compression.

Actual behavior

SyntaxError: Unexpected token in JSON at position 0 since the body isn't decompressed

Expected behavior

Body decompressed from Brotli and JSON is returned

Code to reproduce

I tried this simple code:

const superagent = require('superagent');

superagent
  .get('http://httpbin.org/brotli')
  .set('Accept-Encoding', 'br, gzip, deflate')
  .then(response => {
    console.log('Response Headers:', response.headers);
    console.log('Response:', response.text);
  })
  .catch(error => {
  	console.error('Failed getting Brotli response');
    console.error('Error:', error);
  });

And got SyntaxError: Unexpected token in JSON at position 0 since the body isn't decompressed

When I tried to manually decompress it like that:

const brotli = require('brotli');
const superagent = require('superagent');

superagent
  .get('http://httpbin.org/brotli')
  .set('Accept-Encoding', 'br, gzip, deflate')
  .responseType('arraybuffer') // Get the response as a buffer
  .then(response => {
  	console.log('Response Headers:', response.headers);
    const contentEncoding = response.headers['content-encoding'];

    const decompressedData = brotli.decompress(response.body);

    const responseText = new TextDecoder().decode(decompressedData);
    console.log('Response Data:', responseText);
  })
  .catch(error => {
  	console.error('Failed getting Brotli response');
    console.error('Error:', error);
  });

It worked and I got the proper result:

Response Headers: {
  date: 'Sun, 14 Jul 2024 06:21:12 GMT',
  'content-type': 'application/json',
  'content-length': '173',
  connection: 'close',
  server: 'gunicorn/19.9.0',
  'content-encoding': 'br',
  'access-control-allow-origin': '*',
  'access-control-allow-credentials': 'true'
}
Response Data: {
  "brotli": true, 
  "headers": {
    "Accept-Encoding": "br, gzip, deflate", 
    "Host": "httpbin.org", 
  }, 
  "method": "GET", 
}

I tried using node-fetch and axios and it worked without a need to manually intercept it and Im guessing that behind the scenes you are using Node's http module that have the zlib module that handles gzip and etc... so what am I missing here?

Checklist

  • [V] I have searched through GitHub issues for similar issues.
  • [V] I have completely read through the README and documentation.
  • [V] I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.
@titanism
Copy link
Collaborator

titanism commented Aug 5, 2024

Resolved per #1812

v10.0.0 is released to npm, can you please try it out and let us know if it works OK? 🙏

Thank you for your contribution 🎉

Ref: https://github.com/ladjs/superagent/releases/tag/v10.0.0

@titanism titanism closed this as completed Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants