1

我想做一个非常简单的任务:

  1. 创建一个 http request( npm install request) http://google.com并将其 cookie 保存到cookiejar
  2. 在控制台中查看 cookie 列表cookiejar以验证 cookie 确实存在
  3. 转储所有 cookiecookiejar并得到某种确认它已完成(如console.log('no errors')

浏览了 strong -cookie的文档后,我在第 2 步和第 3 步时遇到了问题,因为 2 只显示了一个空数组,而 3 则抛出了TypeError: undefined is not a function

我的代码如下:

//npm install request
var request = require('request');

//special request that puts cookies in cookiejar
var cookieRequest = request.defaults( { jar : cookiejar } )

//npm install tough-cookie
var tough = require('tough-cookie');

//request the Store API
var store = tough.Store
var Cookie = tough.Cookie;

var cookiejar = new tough.CookieJar();

//load up google.com, I assume google would set some cookies in cookiejar
cookieRequest("http://google.com",function(error, response, body){
    if (!error && response.statusCode==200){
        console.log("requestDone")
    }
});

//request to see the cookies in cookiejar - at the moment it's returning an empty array, 
//would have expected something like ['cookie' : data ]
cookiejar.getCookies("http://google.com",function(err,cookies){
    console.log(cookies)
});

//here I want to dump whatever cookies were stored in cookiejar - at the moment this is 
//throwing a 'TypeError: undefined is not a function' and can't figure out why
cookiejar.store.removeCookies("http://google.com",function(err){
    console.log(err)
});

也仅供参考npm-request的文档

4

1 回答 1

2

我知道这个问题已经有 2 年历史了,但我也一直在试图找出这个问题的答案。我想我知道解决方案。试试这个:

cookiejar._jar.store.removeCookies(...)
于 2018-02-13T16:36:49.543 回答