1

I'm getting GZIP type compressed response from web-service. Can anyone please help me how to decompress or decode the response.

Any help regarding this will be really helpful

Thanks

user87267867
  • 1,409
  • 3
  • 18
  • 25
  • 1
    Have you read http://nodejs.org/api/zlib.html ? – robertklep Mar 19 '13 at 07:24
  • Yes, I tried the example given in the site. But I want response to be displayed in browser. I'm trying to compress json web service – user87267867 Mar 19 '13 at 07:34
  • 2
    Show us your code so far. – robertklep Mar 19 '13 at 07:47
  • http.get('/', function (req, res) { var acceptEncoding = req.headers['accept-encoding']; if (acceptEncoding.match(/\bdeflate\b/)) { res.setHeader('content-encoding', 'deflate'); } else if (acceptEncoding.match(/\bgzip\b/)) { res.setHeader('content-encoding', 'gzip'); } console.log(res._headers); res.end('{"app_id": "A3000990"}'); //JSON data }) })) – user87267867 Mar 19 '13 at 08:52
  • just use `request` or `superagent` – Jonathan Ong Mar 19 '13 at 09:37

1 Answers1

1

It looks as if you want to use Express and the express.compress middleware. That will figure out if a browser supports gzip and/or deflate, so you don't have to.

A simple setup could look like this:

var express = require('express');
var app     = express();

app.use(express.compress());
app.get('/', function(req, res) {
  res.send({ app_id: 'A3000990' });
});
app.listen(3000);

If your data is a JSON-string, you have to set the correct content-type header yourself:

res.setHeader('Content-Type', 'application/json');
res.send(data);
robertklep
  • 198,204
  • 35
  • 394
  • 381
  • When I'm trying to hit the http://localhost:3000 in browser its displaying "Content Encoding Error" – user87267867 Mar 19 '13 at 09:08
  • Using Firefox, I presume? Try another browser, and also see [this page](https://support.mozilla.org/en-US/questions/945251) which might help you fix that issue in Firefox. Or try running it on another port (3001 for example). – robertklep Mar 19 '13 at 09:13
  • Yes in Firefox Content type error is displayed while running in chrome and IE the page cannot be displayed. – user87267867 Mar 19 '13 at 09:16
  • With the exact same code I posted? I tested it on Chrome, Safari, IE10 and FF and it works just fine. – robertklep Mar 19 '13 at 09:18
  • In my client code I'm trying to parse JSON-string using connect.compress() middleware. – user87267867 Mar 19 '13 at 09:22
  • Could you edit your original answer and add the exact code you're using now? – robertklep Mar 19 '13 at 09:37
  • I have posted my code in http://stackoverflow.com/questions/15496042/error-loading-webpage-while-parsing-json-string – user87267867 Mar 19 '13 at 09:48