最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Follow redirect with node.js request - Stack Overflow

programmeradmin0浏览0评论

I'm trying to learn node.js, and I'm working on a utility to log in on a site, and then exctract some info. I have read that redirects should "work automatically" in the documentation, but I can't get it to work.

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);
        });
    }
});

First, I do a POST, which gives a respone.statusCode == 302. The body is empty. I expected the body to contain the redirected page.

Then I found the "new" url, in response.headers['location']. When using that, the body just contains a "not logged in" page, instead of the page I was expecting.

Anyone know of how to go about this?

I'm trying to learn node.js, and I'm working on a utility to log in on a site, and then exctract some info. I have read that redirects should "work automatically" in the documentation, but I can't get it to work.

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);
        });
    }
});

First, I do a POST, which gives a respone.statusCode == 302. The body is empty. I expected the body to contain the redirected page.

Then I found the "new" url, in response.headers['location']. When using that, the body just contains a "not logged in" page, instead of the page I was expecting.

Anyone know of how to go about this?

Share Improve this question asked Sep 12, 2015 at 15:00 kazekaze 4,35914 gold badges55 silver badges75 bronze badges 4
  • action = "login" what this mean? What is your filename ? – I-Kod Commented Sep 12, 2015 at 15:08
  • That's just some form-data that the server expects. – kaze Commented Sep 12, 2015 at 15:13
  • Yes. is it correct with extension? – I-Kod Commented Sep 12, 2015 at 15:15
  • It's not a file reference. Brandon Smith found the acutal problem. Thanks anyway! – kaze Commented Sep 12, 2015 at 15:21
Add a comment  | 

1 Answer 1

Reset to default 33

Redirects are turned on by default for GET requests only. To follow the redirects in your POST, add the following to your config:

followAllRedirects: true

Updated Code:

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);
        });
    }
});
发布评论

评论列表(0)

  1. 暂无评论