1

I have a json file in remote server. <domainName>/info.json. When I navigate to the url I get:

{
  "version": "1.8 RC05"
}

But when I do curl <domainname>/info.json from my terminal, I get something like:

�T*K-*�RP2Գr60Uk�%�c9@

When I use node request as follows:

app.get('/', function(req,res){
    request.get('<domainname>/info.json').on('response', function(data){
        data = data.setEncoding('utf8');
        console.log(data.statusCode);
        res.json(data);
    });
});

I get the following response:

     {
      "statusCode": 200,
      "headers": {
        "content-type": "application\/json; charset=utf-8",
        "content-length": "46",
        "connection": "close",
        "date": "Tue, 22 Sep 2015 09:56:22 GMT",
        "content-encoding": "gzip",
        "cache-control": "max-age=1800, public",
        "last-modified": "Mon, 21 Sep 2015 09:57:35 GMT",
        "etag": "\"93934d435cecbf8f5bde8627903587a0\"",
        "server": "AmazonS3",
        "age": "903",
        "x-cache": "Hit from cloudfront",
        "via": "1.1 44e39d55d481d0cc2faa76f70b7a556b.cloudfront.net (CloudFront)",
        "x-amz-cf-id": "eOy_rb3vasUnhb9bvvKI3AZvcvXAzJeJCI3TmK94ZlaxHu1XwBKzGg=="
      },
      "request": {
        "uri": {
          "protocol": "https:",
          "slashes": true,
          "auth": null,
          "host": "<domainname>",
          "port": 443,
          "hostname": "<domainname>",
          "hash": null,
          "search": null,
          "query": null,
          "pathname": "\/info.json",
          "path": "\/info.json",
          "href": "<domainname>\/info.json"
        },
        "method": "GET",
        "headers": {

        }
      }
    }

I think it has to do something with charset=utf-8, but dont know how to proceed. How do I make my node app get the first json object and serve it?

EDIT 1

As suggested in the answer I served the body as follows:

app.get('/', function(req,res){
    request('<domainname>/info.json', function (error, response, body) {
      if (!error && response.statusCode == 200) {
        console.log(body)
      }
    });
});

Now I get following response:

"\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0003���T*K-*���S�RP2ԳP\br60U��\u0005\u0000k�%�\u001a\u0000\u0000\u0000"
Pravin
  • 1,671
  • 5
  • 23
  • 36

2 Answers2

1

See the Node Request Docs.

You probably want to be sending the body, not the whole response:

app.get('/', function(req,res){
    request('<domainname>/info.json', function (error, response, body) {
      if (!error && response.statusCode == 200) {
        console.log(body)
      }
    })
});

You also probably want to gzip/decompress the response.

For this you can use Nodes Zlib implementation

You can see an exmaple of its use here How do I ungzip (decompress) a NodeJS request's module gzip response body?

Community
  • 1
  • 1
duncanhall
  • 11,035
  • 5
  • 54
  • 86
  • okay, so we have one problem down on the response part. but I am still getting `���T*K-*���S�RP2Գr60U��k�%�` served. – Pravin Sep 22 '15 at 10:36
  • in the browser, this is served `\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0003���T*K-*���S�RP2ԳP\br60U��\u0005\u0000k�%�\u001a\u0000\u0000\u0000` – Pravin Sep 22 '15 at 10:41
  • See the additional info added to my post above. – duncanhall Sep 22 '15 at 10:43
  • i dont have control of the server hosting info.json – Pravin Sep 22 '15 at 10:54
  • You seem to be doing something odd with the response - I can hit the URL and read the response as expected. I can also browse to it and view it. The incorrect responses shown above - I assume these are *after* you have retrieved it and spat it back out? If you *only* `console.log` the response from node, what does the console show? – duncanhall Sep 22 '15 at 11:01
  • I knw right? we can hit the url. I mentioned it at first in the question. but somehow curl and fetching it from node and serving it i get response like that. the `console.log` gives the same jibberish response as well. – Pravin Sep 22 '15 at 11:10
  • i solved it using http://stackoverflow.com/questions/20636587/how-to-decode-gzip-or-utf-8-response-in-node. I did not see it at first. I dont know if it is efficient way – Pravin Sep 22 '15 at 11:16
  • yeah i found a similar answer on mentioned link. Thanks anyway. – Pravin Sep 22 '15 at 11:24
  • anyway, the answer is the answer you mentioned, so I have upvoted and selected your answer. – Pravin Sep 23 '15 at 04:01
0

Try this:

console.log(body.toString('ascii'))
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68