Setting user-agent with node js

8,862 views
Skip to first unread message

santa

unread,
Jul 21, 2012, 2:42:28 AM7/21/12
to nod...@googlegroups.com
Hello All,

I am trying to use superagent to request data from a server. I believe the problem is setting the 'user-agent' in the request but I am having a tough time figuring out the problem. I get an access denied error.

For example from the shell first:

This gives access denied

B) curl --user-agent 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13' 'http://www.nseindia.com/live_market/dynaContent/live_watch/stock_watch/juniorNiftyStockWatch.json'
This works!

However when I use SuperAgent it fails. Can someone please help me fix this?
I am using the following code:

   var request = require('superagent');
   request
       .set('user-agent','Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13')
       .end(function(res){
       if (res.ok) {
        console.log(res.text);
       }
       else {
          console.log(res);
          }
       });


Thanks,
Santosh

mscdex

unread,
Jul 21, 2012, 6:56:38 AM7/21/12
to nod...@googlegroups.com
On Saturday, July 21, 2012 2:42:28 AM UTC-4, santa wrote:
I am trying to use superagent to request data from a server. I believe the problem is setting the 'user-agent' in the request but I am having a tough time figuring out the problem. I get an access denied error.

Did you try using the core HTTP methods?

Example (untested):

var http = require('http');

http.get({
  headers: {
    'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'
  }
}, function(res) {
  if (res.statusCode === 200) {
    var body = [];
    res.setEncoding('utf8');
    res.on('data', function(chunk) {
      body.push(chunk);
    });
    res.on('end', function() {
      body = body.join('');
      console.log(body);
    });
  } else {
    console.log('Received non-ok response: ' + res.statusCode);
  }
});

mscdex

unread,
Jul 21, 2012, 6:58:06 AM7/21/12
to nod...@googlegroups.com
On Saturday, July 21, 2012 6:56:38 AM UTC-4, mscdex wrote:
Did you try using the core HTTP methods?

Additionally, did you try comparing the exact headers sent by cURL and those sent by superagent (using Wireshark or something else)?

santa

unread,
Jul 21, 2012, 7:04:16 AM7/21/12
to nod...@googlegroups.com
Tested it. Doesnt work. Same problem.
Not sure how to go about it.
Reply all
Reply to author
Forward
0 new messages