Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set user-agent in nodejs?

Tags:

node.js

I tried this, but I get an error.

http.get({ url: 'http://www.panda.tv/ajax_chatinfo?roomid=89757',
    agent: 'Mozilla/5.0' }, function(res) {
    res.on('data', function(chunk) {
        doSomething();
    });
});

I read the API documentation, but I didn't find anything on how to create.

When I run the code, I get the following error:

_http_client.js:158
    self.agent.addRequest(self, options);
               ^

TypeError: self.agent.addRequest is not a function
    at new ClientRequest (_http_client.js:158:16)
    at Object.exports.request (http.js:31:10)

    at Object.exports.get (http.js:35:21)
    at getChatInfo (/home/zeek/Documents/pandatv/node/app.js:5:10)
    at Object.<anonymous> (/home/zeek/Documents/pandatv/node/app.js:153:1)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
like image 766
user3130007 Avatar asked Aug 05 '16 12:08

user3130007


1 Answers

const options = {
  hostname: 'www.panda.tv',
  path: 'ajax_chatinfo?roomid=89757',
  headers: { 'User-Agent': 'Mozilla/5.0' }
};

http.get(options, function(res) {
  res.on('data', function(chunk) { console.log(chunk) });
});

Consider next npm modules request or superagent, they will help you much with processing http requests. Drop a comment if you need examples, but I'm sure you'll get it from documentations. GL.

like image 59
evilive Avatar answered Sep 22 '22 00:09

evilive