Transfer / pass cookies from one request to anothe

2019-07-25 21:06发布

问题:

I am using request library to make http requests to the server (https://github.com/request/request).

Unfornatelly I got 400 response, because of some data is missing in my request (probably cookies).

This is how I am doing it:

 let request = require('request');
 request.post("https://some-server.com/asyncUserLogin", {
           form: {
                username: 'rapidtest',
                password: 'rapidtest123',
                TARGET_URL: targetUrl
           },
           jar: jar1
      }, (err, res, body) => {
           /*
            * I am trying to get cookies from this request 
            * and pass it to the next in multiple ways, 
            * here I am stripping cookie manually, 
            * and trying to set Cookie: header 
            */
           let cookies = jar1.getCookies("https://some-server.com/asyncUserLogin");
           let cookiesString: string = "";
           cookies.forEach(cookie => {
               if(cookiesString.length > 0)
                   cookiesString += ";"
                cookiesString += cookie.key + "=" + cookie.value;
            });

            /*
             * in this request I am trying to send POST to: 
             * https://some-server.com/getOauthCookie?grant_type=password_assertion&client_id=xxx-yyyy   
             * along with cookies retrieved in previous request, 
             * it should respone with 200 and some other cookies 
             */
            request.post("https://some-server.com/getOauthCookie", {
                har: {
                    postData: {
                        mimeType: 'application/x-www-form-urlencoded',
                        params: [
                            {
                                name: 'grant_type',
                                value: 'password_assertion'
                            },
                            {
                                name: 'client_id',
                                value: 'xxx-yyyy'
                            }
                        ]
                    },
                    headers: [
                         {
                             name: 'Content-Type',
                             value: 'application/x-www-form-urlencoded'
                         },
                         {
                             name: 'Cookies',
                             value: cookiesString
                         }
                    ],
                }
                jar: jar1
            }, (err, res, body) => {
           //this request fails
      });
 });

What should I do, to be sure, that cookies from first request are passed to the second one?

How can I display request which I am doing, to chceck if something is missing/malformed (How to view request sent from node.js to server?)?

回答1:

You can access the complete request from the full response body - response.request and then extract your cookie value from the request headers

request(options, function (error, response, body) {
    if (error) throw new Error(error);
    // This will give you complete request
    console.log(response.request);
    //This gives you the headers from Request
    console.log(response.request.headers['Cookies']);

});