0

I am trying to redirect to another url immediately after my 1st url request is completed. The reason for that is because in order to open 2nd url, it requires the 1st url session cookie. It means 2nd url can only open if I open the 1st url prior to that action.

var request = require('request');
var j = request.jar();

request({
    url: first_url,
    jar: j,
    json: true
}, function (error, response, body) {
    if (!error && response.statusCode === 200) {
        var cookies = j.getCookieString(first_url);
        console.log("1st url cookie: "+ cookies);
        request(second_url, function(body) {
            var cookies2 = j.getCookieString(second_url);
            console.log("2st url cookie: "+ cookies2);
            console.log(body);
        })

I modified my code as shown above to pass session cookies. However, my 2nd URL still cannot be opened. My console output shows both urls have the same session id, so I only assume the session cookie is passed correctly.

1st url cookie: JSESSIONID=DD4ACC4D65F024BD06D53A648A5B4DE3

2st url cookie: JSESSIONID=DD4ACC4D65F024BD06D53A648A5B4DE3

{ [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }

The way I verified session cookie is required on my project is to open 1st URL on browser first, then copy and paste the 2nd URL link to open the 2nd link. Without launching the 1st URL, 2nd link will not open and eventually timeout the request.

Please advice again and maybe I am not looking at the right direction.

RogerMW
  • 95
  • 8
  • Possible duplicate of [How to maintain a request session in NodeJS](http://stackoverflow.com/questions/19936705/how-to-maintain-a-request-session-in-nodejs) – Yonghoon Lee Feb 22 '17 at 02:45

1 Answers1

0

response.redirect() is the wrong choice here. First off, that's not used in this context. It is used when someone is making a web request TO you and you want to tell them to request a different page. When that's the case, you send a 30x status back and a new location and the other end, then follows that redirect and requests a new page. That is the opposite of what you are doing.

What you need to do is to make a request to the first URL, establish the session, capture the cookies from that request. Then, using those same cookies, make a request to the 2nd URL. With the cookies attached, the second request should be treated as being logged in.

The concept of keeping track of cookies on your server when making requests to another site is often called a cookie jar and there are many NPM modules to integrate with the request() module to preserve your login cookies. In fact, the request module actually has it's own cookie jar abilities built-in. Cookie Jar examples here.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Hi, thank you so much for your clear explanation and suggestion. Please refer to my modified code and comment on the original topic field and hope you can give some suggestion. Many thanks. – RogerMW Feb 22 '17 at 07:46
  • @user4010075 - Your second request needs to use the same cookie jar, just like the first request is. This is the whole point - to have a single cookie jar that both requests use. – jfriend00 Feb 22 '17 at 07:52
  • You are correct! Creating a second request using same cookie jar solved my problem. Thank you so much for your help. – RogerMW Feb 23 '17 at 01:47