2015-01-26 3 views
2

Request, Jsdom 및 NodeJS를 사용하여 웹 페이지에 로그인하고 있습니다. Request api에 따르면Request 및 NodeJS를 사용하여 리디렉션 된 페이지로 이동하는 방법

followRedirect - HTTP 3xx 응답을 리디렉션 (기본값 : true)으로 따릅니다. 이 속성은 응답 객체를 단일 인수로 가져 오는 함수로 구현 될 수 있으며 리디렉션이 계속되어야한다면 true를 반환하고 그렇지 않으면 false를 반환해야합니다.

기본값 인 true로 지정하면 중간에 리디렉션 페이지가 아닌 리다이렉트 페이지를 요청할 것이라고 생각합니다.

내 코드 :

request(loginPageURL, function(error, response, body) { 
    /*Error handling removed for brevity*/ 
     jsdom.env({ 
      html: body, 
      scripts: [ 
       'http://code.jquery.com/jquery-1.7.1.min.js' 
      ], 
      done: function(err, window) { 
       //Set login fields 
       var form = $('#authorizationForm'); 
       var data = form.serialize(); 
       var url = form.attr('action') || 'get'; 
       var type = form.attr('enctype') || 'application/x-www-form-urlencoded'; 
       var method = form.attr('method'); 

       request({ 
        url: url, 
        method: method.toUpperCase(), 
        body: data, 
        headers: { 
         'Content-type': type 
        } 
       }, function(error, response, body) { 
        if (!error) { 
         console.log("Response.statusCode:" + response.statusCode); 
         console.log("Body:" + body); 

대신에 로그인 한 후 나에게 홈 페이지를주는, 출력은 다음과 같습니다 리디렉션 된 페이지의 코드를 얻는 것이 유용 할 수도 있지만

Response.statusCode: 302 
Body: To access this site, go to <a href="..."> 

, 수행하는 방법 내가 리디렉션하고있는 페이지로 건너 뜁니 까?

답변

1

GET 요청이 아닌가요? 그런 다음 명시 적으로 followAllRedirects: true을 설정해야합니다.

followAllRedirects - 리디렉션 비 GET HTTP 3xx의 응답 (기본값 : false)에 따라

https://github.com/request/request

(그들은 있음을 표시하지 않는 것 때문에 followRedirect에 대한 문서가 혼란을 followRedirect GET에만 적용됩니다.)

+0

감사합니다. @Emmett! 인증/쿠키 설정으로 인해 jars에 대한 요청 defualts를 true로 설정해야했습니다. – BluestTitan