0

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
}
Community
  • 1
  • 1
naorz
  • 349
  • 3
  • 6
  • What is the actual error? – DF_ Aug 06 '14 at 00:37
  • actually i got the response (from my request) but inside the call back function the functions that not working (also gunzip.on('error'.... ) not working, moreover also the functions gunzip / deflate . on: 'data' / 'end' / 'error' not working, the debugger not going in there. now i dont know if I was able to parse the information or not – naorz Aug 08 '14 at 17:03
  • ok, now i used: [needle](https://github.com/tomas/needle) and its do the pipe thing (enter to gunzip and deflate) but to the 'end' function and the 'data' variable is undefined P.S. i put all methods of zlib outside from the call back function with all root functions – naorz Aug 16 '14 at 12:16

0 Answers0