Frage

I have a node proxy server in which I am passing through a response from another server. It is returning xml. The resultant XML makes it to the browser but when trying to intercept the response in the proxyResponse.on event, I cant seem to parse out the data. Trying to use chunk.toString() results in unreadable text.

            // Handle proxy response data
            proxyResponse.on (
                'data',
                function ( chunk ){

                    if ( debugging ){
                        console.log ( '  < chunk = %d bytes', chunk.length );
                    }
                    xml += chunk.toString();
                    response.write ( chunk, 'utf8');    

                    console.log('Response: '+ xml);
                }
            );
War es hilfreich?

Lösung

The http headers was the clue that lead to my solution. The chunk was being compressed by Gzip, so I used the Zlib library to unzip the chunk in order to get at the xml inside.

var zlib = require('zlib');
// Handle proxy response data
proxyResponse.on (
   'data',
   function ( chunk ){

       if ( debugging ){
           console.log ( '  < chunk = %d bytes', chunk.length );
   }
   xml += chunk;
   response.write ( chunk );    
   zlib.unzip(chunk, function(err, chunk){
   if (!err){
       console.log('Response'+chunk.toString())
   }
   });
});
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top