简体   繁体   中英

Login to another site from nodejs server

I am trying to login into a site to scrap the data. Login works fine if try to do it through Chrome Rest Client but from node server I am getting status code 302 from the website.
Code

var http = require('http'),
    express = require('express'),
    request = require('request');



var app = express();
    app.listen(3000);

var request = request.defaults({jar:true});
app.post('/login', [express.urlencoded(), express.json()], function(req, res) {
    console.log('You are here'); console.log(JSON.stringify(req.body.email));
    request.post({
      url: 'http://teeSpring.com/login/submitLogin',
      form: {
        email: "himanshu",
        password: "himanshu"
      }
    }, function(error, resposne, body) {
      if (error) {
        console.log('Error is: '+JSON.stringify(error));  
      } else if(body){
        console.log('Body is :')//+JSON.stringify(body));
      } else if(resposne) {
        console.log('Response is :'+resposne.statusCode);//+JSON.stringify(resposne));
        res.send(resposne);
      } else {
        console.log('WTF');
      }

    });

});

You could do something like this - if redirected, get headers location and go there:

  else if(response) {
    console.log('Response is :'+response.statusCode);//+JSON.stringify(response));
    if(response.statusCode == 302 && response.headers && response.headers.location) {
      res.send(response.headers.location);
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM