遵循与node.js请求redirect

我试图学习node.js,我正在开发一个工具来login一个网站,然后提取一些信息。 我已经读过redirect应该在文档中“自动工作”,但是我不能使它工作。

request({ url: start_url, method: 'POST', jar: true, form: { action: 'login', usertype: '2', ssusername: '****', sspassword: '****', button: 'Logga in' } }, function(error, response, body) { if (error) { console.log(error); } else { console.log(body, response.statusCode); request(response.headers['location'], function(error, response, html) { console.log(html); }); } }); 

首先,我做了一个POST,它给出了一个respone.statusCode == 302.正文是空的。 我期望身体包含redirect页面。

然后,我在response.headers ['location']中find了“新”url。 当使用它时,主体只包含一个“未login”页面,而不是我期望的页面。

任何人都知道如何去做这个?

redirect仅在GET请求时被默认打开。 要遵循POST中的redirect,请将以下内容添加到您的configuration中:

 followAllRedirects: true 

更新的代码:

 request({ url: start_url, method: 'POST', followAllRedirects: true, jar: true, form: { action: 'login', usertype: '2', ssusername: '****', sspassword: '****', button: 'Logga in' } }, function(error, response, body) { if (error) { console.log(error); } else { console.log(body, response.statusCode); request(response.headers['location'], function(error, response, html) { console.log(html); }); } });