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']);
    });
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top