setCookies doesn't work with new version tough-cookie
Opened this issue · 3 comments
cogor commented
I saving cookies via getCookies()
function
When I try to restore then, I getting an error "Error: First argument to setCookie must be a Cookie object or string"
I downgraded version of tough-cookie
to 4.1.2(was v5.0.0) and now it works well
karashiiro commented
Can you share how you're storing and loading cookies? I'm seeing this comment under the branch for that error in v5 of the library:
} else if (!(cookie instanceof Cookie)) {
// If you're seeing this error, and are passing in a Cookie object,
// it *might* be a Cookie object from another loaded version of tough-cookie.
const err = new Error(
'First argument to setCookie must be a Cookie object or string',
)
return options?.ignoreError
? promiseCallback.resolve(undefined)
: promiseCallback.reject(err)
}
cogor commented
I storing cookies in json file
// Saving
const cookies = this.scraper.getCookies();
const serializedCookies = JSON.stringify(cookies, null, 2);
writeFileSync(this.COOKIE_FILE, serializedCookies, 'utf8');
// Restoring
const fileContent = readFileSync(this.COOKIE_FILE, 'utf-8');
const cookies = JSON.parse(fileContent).reduce((acc: Cookie[], c) => {
const cookie = Cookie.fromJSON(c);
return cookie ? [...acc, cookie] : acc;
}, []);
this.scraper.setCookies(cookies);
scinscinscin commented
to anyone finding this, you can find out what version of tough-cookie the library is using by running yarn why tough-cookie
, then simply match your project's against that version and it should work.