简体   繁体   中英

Why isn't this attempt to log into reddit using superagent on Node.js successful?

Here's the most minimal test case that I could reduce. It may help to convert it to use native Node HTTP or else use the request library, if you're more familiar with either one of those. But as is, I get back a bunch of jquery crap. AFAICT, the HTTP POST request sent is identical to the one programmed in curl here: https://stackoverflow.com/a/15169425/3025492

var USER = 'uuuut',
    PASS = 'ppppt';
superagent
    .post( 'https://pay.reddit.com/api/login/' )
    .send( { api_type: 'json', rem: 'True',
              user: USER, passwd: PASS } )
    .end( function( res ) {
      console.log( 'Session cookie: ', res.body.data.cookie || res.headers['Set-Cookie'] );
    });

When done correctly, it's just supposed to set an authentication cookie.

There are several problem in your code:

  1. the post request in form instead of json , so you need the method .type('form')
  2. the returned cookie at: res.body.json.data.cookie
  3. headers in superagent must be in lower case: res.headers['set-cookie']

Full code:

superagent
    .post( 'https://pay.reddit.com/api/login/' )
    .type('form') // send request in form format
    .send( { api_type: 'json', rem: 'True',
              user: USER, passwd: PASS } )
    .end( function(err, res) {
      console.log( 'Session cookie: ', res.body.json.data.cookie || res.headers['set-cookie']);
    });

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