转移/传递从一个请求中的Cookie到另一个的NodeJS /量角器(Transfer / pass

2019-09-25 17:08发布

我使用的request库来进行HTTP请求到服务器( https://github.com/request/request )。

Unfornatelly我得到了400的响应,因为一些数据在我的要求(可能饼干)丢失。

这是我正在做它:

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

我应该怎么做,可以肯定的,从第一个请求的cookie被传递到第二个?

我怎样才能显示它要求我做,到chceck如果缺少了什么/畸形( 如何查看请求从的node.js发送到服务器? )?

Answer 1:

您可以从全响应主体访问完整的请求- response.request ,然后从请求头中提取您的cookie值

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']);

});


文章来源: Transfer / pass cookies from one request to another in nodejs/protractor