salesforce / tough-cookie

RFC6265 Cookies and CookieJar for Node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to get raw string from `new tough.CookieJar()`

longhoangwkm opened this issue · comments

Hi, I would like to get raw string of cookie from new tough.CookieJar(). I tried to use https://github.com/salesforce/tough-cookie#cookiestring but not works!

cookies.json

[
  {
    "name": "session-id",
    "value": "146-2080403-0122767",
    "domain": ".exampe.com",
    "path": "/",
    "expires": 2082787201.130887,
    "size": 29,
    "httpOnly": false,
    "secure": false,
    "session": false,
    "priority": "Medium"
  },
  {
    "name": "i18n-prefs",
    "value": "USD",
    "domain": ".exampe.com",
    "path": "/",
    "expires": 2082787201.957653,
    "size": 13,
    "httpOnly": false,
    "secure": false,
    "session": false,
    "priority": "Medium"
  },
  {
    "name": "ubid-main",
    "value": "130-3172631-1660911",
    "domain": ".exampe.com",
    "path": "/",
    "expires": 2082787201.130592,
    "size": 28,
    "httpOnly": false,
    "secure": false,
    "session": false,
    "priority": "Medium"
  }
]
  const cookieJar = new tough.CookieJar();
  const cookiesPath = './cookies.json'
  const buf = fs.readFileSync(cookiesPath);
  const cookies = JSON.parse(buf);
  cookieJar.setCookie(JSON.stringify(cookies), 'https://www.example.com');
  console.log(cookieJar.cookieString()); // NOT WORKS!

 // Expected: "session-id=146-2080403-0122767; i18n-prefs=USD; ubid-main=130-3172631-1660911;"

Hi @longhoangwkm, apologies for the late response. .cookieString() is a function defined on a cookie; you're trying to call it on a CookieJar. Please try .getCookieString() instead.

Additionally, it doesn't look like your cookie string should be parsed correctly - cookie strings have to conform with RFC 6265: https://tools.ietf.org/html/rfc6265#section-4.1.1