8

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.

1
  • 1
    I realized what the problem is.. I wasn't using the http.client to make the request. I guess the client handles the keepalive response and the cookies and abstracts it away. HTTP GET sends a new request each time, and the state is not maintained between requests. So fb.com must be doing something like, if cookie not set redirect to fb.com and set the cookie. I might need some more enlightenment here. But moral of the story... use http.client to make requests. You will get the correct behavior Commented Aug 8, 2011 at 7:37

2 Answers 2

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

Setting the request option this way should work.

Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.