9

I am using Axios to get the JSON response from the web server. The response is in compressed gzip format. How can I decompress the response and get the Json Data.

1

2 Answers 2

9

axios has a decompress option. No need to decompress it manually:

const { data } = await axios.get(url, { responseType: 'arraybuffer', 'decompress': true })

Also, your server should never sent compressed content if your Accept-Encoding header does not contain gzip (or any other compression format).

1
  • Axios sends an accept-encoding header of 'gzip, deflate, br' by default because browsers normally decompress the response but that creates problems with node where that's not the case.
    – Xetera
    Dec 5, 2022 at 1:51
8
const zlib = require('zlib')

let url = "https://example.com/GZ_FILE.gz"

const { data } = await axios.get(url, { responseType: 'arraybuffer' })

zlib.gunzip(data, function (_err, output) {
    console.log(output.toString())
  })

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.