Utilizing the request module in node.js makes it simple to create a request that can retrieve and correctly decompress compressed data from the source:
var request = require('request');
var requestOptions = {
url: 'http://whatever.com/getDataWithCompression',
gzip: true // <--- only this is necessary
};
request(
requestOptions,
function (error, response, data) {
// manipulate data (already decompressed)
}
);
However, I have some JavaScript code embedded within an HTML document that also needs to perform an HTTP request. In this case, without the node.js request
module, I am resorting to using XMLHttpRequest
:
var request = new XMLHttpRequest();
request.open('GET', 'http://whatever.com/getData', true);
request.onload = function() {
// perform operations on request.responseText
};
request.send();
Despite extensive searching online, I am struggling to figure out how to make an HTTP request with XMLHttpRequest
and handle gzipped data compression. Any assistance would be greatly appreciated.