What is decompress-response?
The decompress-response npm package is used to decompress HTTP responses that are compressed. This is useful when making HTTP requests and the server responds with compressed data, such as gzip or deflate. The package automatically detects the encoding and decompresses the response so that it can be easily processed in its uncompressed form.
What are decompress-response's main functionalities?
Decompressing HTTP responses
This code sample demonstrates how to use decompress-response to decompress an HTTP response. The 'http.get' method is used to make a GET request to a server. The response is then passed through the decompressResponse function, which returns a stream with the decompressed response. Finally, the decompressed data is piped to the standard output.
const http = require('http');
const decompressResponse = require('decompress-response');
http.get('http://example.com', (res) => {
res = decompressResponse(res);
res.pipe(process.stdout);
});
Other packages similar to decompress-response
pump
The pump package is a small node module that pipes streams together and destroys all of them if one of them closes. While it does not provide decompression functionality by itself, it can be used in conjunction with stream-compression modules to handle errors and clean up streams properly after decompression.
got
Got is a human-friendly and powerful HTTP request library for Node.js. Got supports decompression of HTTP responses out of the box, similar to decompress-response, but it also provides a much richer set of features for making HTTP requests, such as retries, pagination, and more.
node-fetch
node-fetch is a light-weight module that brings the Fetch API to Node.js. It allows you to make HTTP requests similar to the way you would in a browser. The Fetch API supports transparent decompression of responses, which is a feature also provided by decompress-response, but node-fetch offers a broader feature set for making requests and handling responses.
axios
Axios is a promise-based HTTP client for the browser and Node.js. It supports response data decompression, much like decompress-response. However, axios provides a wide range of features such as interceptors, automatic transforms for JSON data, and client-side protection against XSRF.
decompress-response
Decompress a HTTP response if needed
Decompresses the response from http.request
if it's gzipped, deflated or compressed with Brotli, otherwise just passes it through.
Used by got
.
Install
npm install decompress-response
Usage
import http from 'node:http';
import decompressResponse from 'decompress-response';
http.get('https://sindresorhus.com', response => {
response = decompressResponse(response);
});
API
decompressResponse(response)
Returns the decompressed HTTP response stream.
response
Type: http.IncomingMessage
The HTTP incoming stream with compressed data.