I tried a lot of time to make request from nodeJS server.
my service is a public one (the service out of my control)
i tried to look up solutions but I don't know what i'm doing wrong :\
i dont know if its matter but the response body contain few words in hebrew
With this article I tried to solve my problem
Full code from article is here
I'd love to get help to solve my problem :)
UPDATE:
here is my new code: (but still not working - explanation inside the code)
var express = require('express');
var app = express();
var port = 3429;
var request = require('request');
var zlib = require('zlib');
new lib that make the pipe thing worked:
var needle = require('needle');
then we have little bit changes:
var json = "";
var deflate = zlib.createInflate();
var gunzip = zlib.createGunzip();
// options definitions
var url = 'MY_SERVICE_URL';
var headersOptions = {
'Accept-Encoding': 'gzip,deflate,sdch' // encoding
, 'Content-Type': 'text/plain; charset=utf-8' // content type
, 'Accept-Language': 'en-US,en;q=0.8,he;q=0.6' // language
};
var options = {
compressed: true
, headers: headersOptions
};
app.listen(port, function () {
console.log("Listening on " + port);
setTimeout(function () {
needleTest();
}, 5000);
});
var needleTest = function () {
needle.get(url, options, function (err, response, body) {
debugger;
console.log(response.body);
response.pipe(gunzip);
response.pipe(deflate);
});
};
response.body is:
��{
"id" : "39",
"title" : "���",
"data" : []
}
after it: response.pipe(gunzip) and response.pipe(deflate) its goes to zlib method 'end'
the problem now is: the variable of callback function is undefined
zlib methods:
/* zlib ===> deflate */
deflate.on('data', function (data) {
debugger;
console.log('deflate data');
console.log(data);
json += data.toString();
});
deflate.on('end', function (data) {
debugger;
console.log('deflate end');
console.log(data);
parseJSON(json);
});
deflate.on('error', function (data) {
debugger;
console.log('deflate error');
console.log(data);
});
/* zlib ===> gunzip */
gunzip.on('data', function (data) {
debugger;
console.log('gunzip data');
console.log(data);
json += data.toString();
});
gunzip.on('end', function (data) {
debugger;
console.log('gunzip end');
console.log(data);
parseJSON(json);
});
gunzip.on('error', function (data) {
debugger;
console.log('gunzip error');
console.log(data);
});
function parseJSON(json) {
// do the parse things
}