Search code examples
httpnode.jshttpsgzipdeflate

Easy HTTP requests with gzip/deflate compression


I'm trying to figure out how the best way to easily send HTTP/HTTPS requests and to handle gzip/deflate compressed responses along with cookies.

The best I found was https://github.com/mikeal/request which handles everything except compression. Is there a module or method that will do everything I ask?

If not, can I combine request and zlib in some manner? I tried to combine zlib and http.ServerRequest, and it failed miserably.


Solution

  • Note: as of 2019, request has gzip decompression built in. You can still decompress requests manually using the below method.

    You can simply combine request and zlib with streams.

    Here is an example assuming you have a server listening on port 8000 :

    var request = require('request'), zlib = require('zlib');
    
    var headers = {
        'Accept-Encoding': 'gzip'
    };
    
    request({url:'http://localhost:8000/', 'headers': headers})
        .pipe(zlib.createGunzip()) // unzip
        .pipe(process.stdout); // do whatever you want with the stream