Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clone Cookie Jar / Get all Cookies from Cookie Store #29

Closed
seanstrom opened this issue Mar 5, 2015 · 6 comments
Closed

Clone Cookie Jar / Get all Cookies from Cookie Store #29

seanstrom opened this issue Mar 5, 2015 · 6 comments

Comments

@seanstrom
Copy link

Based on a discussion here request/request#1458
We have a situation where we want to clone a CookieJar. I'm not completely clear on the "why" we want to do this, but we started to explore the "how". I'm wondering if anyone here has any recommendation on how to clone a CookieJar or at least how to get all the cookies from a CookieJar so we can manually clone it.

@lucaswxp
Copy link

+1

I need to be able to serialize a cookie jar and recreate it later in another server. Is this possible? Getting all the cookies from a jar is a start..

Right now I'm trying to inspect the jar.store property, but without much success.

@lucaswxp
Copy link

After some time trying, I came up with these three functions:

var Cookie = tough.Cookie,
      CookieJar = tough.CookieJar;

function cookieJarToJSON(jar){
  return JSON.stringify(jar.store.idx);
}

function cookieJarFromJSON(json){
  var idx = JSON.parse(json),
      jar = new CookieJar;

  for(var i in idx){
    for(var o in idx[i]){
      for(var cookieKey in idx[i][o]){
        var c = idx[i][o][cookieKey];
        jar.setCookieSync(new Cookie(c).toString(), {hostname: c.domain, path: c.path});
      }
    }
  }

  return jar;
}

function cookieJarClone(jar){
  return cookieJarFromJSON(cookieJarToJSON(jar));
}

If you want a clone of a jar, just do clonedJar = cookieJarClone(jar), also if you need to store a jar as string, use cookieJarToJSON(jar) and to deserialize cookieJarFromJSON(str).

@inikulin
Copy link
Contributor

Seems can be implemented in context of #24

@inikulin
Copy link
Contributor

PR is welcome, BTW

@stash-sfdc
Copy link
Contributor

I'm going to try implement a jar-to-JSON feature today.

@stash-sfdc stash-sfdc self-assigned this May 25, 2015
@inikulin
Copy link
Contributor

Fixed via #42, published as v2.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants