0
votes

Okay I'm trying to get the headers of this specific URL and node.js behaviour baffles me.

My code :

var http = require('http');

var req = http.get("http://listen.radionomy.com/abc-lounge", function(res) {
   console.log("headers: ", res.headers); 
});

Prints out :

headers: { 'cache-control': 'private', 'content-type': 'text/html; charset=utf-8', server: 'Microsoft-IIS/7.5', 'x-aspnet-version': '4.0.30319', 'x-powered-by': 'ASP.NET', date: 'Tue, 28 Jan 2014 14:18:27 GMT', 'content-length': '8309' }

Now I tried out the command line curl with headers :

curl -I http://listen.radionomy.com/abc-lounge

This prints out exactly what I'm looking for (The redirect url) :

HTTP/1.1 302 Found
Cache-Control: private
Content-Length: 0
Content-Type: application/octet-stream
Location: http://streaming.radionomy.com/ABC-Lounge
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 5.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 28 Jan 2014 14:19:20 GMT

I don't understand why node is getting a different set of headers. It should not follow redirects by default. I don't even think this is a redirect issue.

2

2 Answers

2
votes

This is because http.get actually follows the redirect

0
votes

I fake a curl request by adding some headers to your http.get like this:

var http = require('http');

var options = {
    host: 'listen.radionomy.com',
    path: '/abc-lounge',
    headers: {
        'user-agent': 'curl/7.31.0',
        'accept': '*/*'         
    }
};

var req = http.get(options, function(res) {
   console.log('status:', res.statusCode)
   console.log("headers: ", res.headers); 
});

Output will be:

status: 302
headers:  { 'cache-control': 'private',
  'content-type': 'application/octet-stream',
  location: 'http://streaming.radionomy.com/ABC-Lounge',
  server: 'Microsoft-IIS/7.5',
  'x-aspnetmvc-version': '5.0',
  'x-aspnet-version': '4.0.30319',
  'x-powered-by': 'ASP.NET',
  date: 'Tue, 28 Jan 2014 15:02:16 GMT',
  'content-length': '0' }