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"
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?