请求模块:如何将收集的cookie存储在变量中?

时间:2018-08-19 13:37:23

标签: javascript node.js cookies request

模块:https://www.npmjs.com/package/request

我正在尝试同时登录网站上的多个帐户。因此,为了跟踪每个会话,我将创建一个对象,在该对象中存储每个帐户的关联cookie。

现在,如何将收集的cookie存储在变量中?

   var request = require('request');
   var request = request.defaults({jar: true, headers: eo.data.headers});

   request.post(options, (err, res, body) => {
               var cookies = *SOMETHING* 
               //How do I save the cookies after I have succesfully logged into the website

1 个答案:

答案 0 :(得分:1)

尝试一下:

const jar = request.jar();
const cookies = res.headers['set-cookie'] || [];
cookies.forEach(c => {
    jar.setCookie(c2, <that_url>);
});

jar保存到数组或可以获取的内容中。