将cookie从一个请求传输/传递到nodejs/protractor中的另一个请求 [英] Transfer / pass cookies from one request to another in nodejs/protractor

查看:52
本文介绍了将cookie从一个请求传输/传递到nodejs/protractor中的另一个请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 request 库向服务器发出http请求( https://github.com/request/request ).

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

不幸的是,由于请求中缺少某些数据(可能是cookie),我得到了400条回复.

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

这就是我的做法:

 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传递给第二个?

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?)?

推荐答案

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

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

});

这篇关于将cookie从一个请求传输/传递到nodejs/protractor中的另一个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆