Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with http GET request on node js

Tags:

http

node.js

Im writing an http client to read from facebook using node.js and using the following code:

var http = require('http');

var options = {
  host: 'www.fb.com',
  path: '/',
  "user-agent": "node.js"
};

http.get(options, function(res) {
  console.log("Got response: " + res.statusCode);
  console.log(res.headers);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});

In my browser, this returns a 301 redirect with the location of www.facebook.com, however in node.js I get a 302 response with a location of www.fb.com/common/browser.php. I tried it with the latest version of node and it still throws this error.

I'd really appreciate some help with this, Thanks.

like image 398
user868459 Avatar asked Jul 30 '11 08:07

user868459


2 Answers

var request_options = 
{
    host: 'www.fb.com',
    headers: {'user-agent': 'Mozilla/5.0'},
    path: '/'
};

Setting the request option this way should work.

like image 79
abdallah Avatar answered Oct 31 '22 04:10

abdallah


This dosent seem to be a bug of node.js I made a request to fb.com in curl and I got the same redirect.

It probably makes that decision based on the user agent... maybe you can use the user agent of a browser :D

like image 34
megakorre Avatar answered Oct 31 '22 04:10

megakorre