这是我可以减少的最小测试用例。如果您更熟悉其中任何一种,将其转换为使用 native Node HTTP 或使用请求库可能会有所帮助。但事实上,我得到了一堆 jquery 垃圾。 AFAICT,发送的 HTTP POST 请求与此处在 curl 中编程的请求相同: 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'] );
});
如果正确完成,它应该设置一个身份验证 cookie。
最佳答案
您的代码中有几个问题:
- post请求使用
form
而不是json
,所以你需要方法.type('form')
- 返回的 cookie 位于:
res.body.json.data.cookie
- superagent 中的 header 必须为小写:
res.headers['set-cookie']
完整代码:
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']);
});
关于javascript - 为什么在 Node.js 上使用 superagent 尝试登录 reddit 失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21213534/