question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get a value of a cookie provided a cookie jar?

See original GitHub issue

It is not entirely clear from the documentation how to get a value of a specific cookie provided an instance of a CookieJar.

// This always gives me a single item array.
// It is not clear from the documentation when the result would contain more than entry.
const cookies = jar.getCookiesSync(guide.result.url);

> [ Cookie="ASP.NET_SessionId=gqwdcucn4iqdztwcvqr3xmpy; Expires=Wed, 17 Mar 2027 16:00:50 GMT; Domain=foo.com; Path=/; HttpOnly; HttpOnly,SC_ANALYTICS_GLOBAL_COOKIE=4e3659e87284422c8dd691f35b1a622e|False; HttpOnly,UserSessionId=af5abf42-e3c9-4478-a1c7-61300614dc8c|10002|181845|161471|2017-03-17; hostOnly=false; aAge=0ms; cAge=2ms" ]

// This is supposed to parse the cookie-string into a `Cookie` object.
Cookie.parse(cookies[0]);

Using Cookie.parse in the above example I am getting an error:

TypeError: str.trim is not a function at Function.parse (/Users/gajus/Documents/dev/foo/node_modules/tough-cookie/lib/cookie.js:325:13) at exports.scrapeTickets (/Users/gajus/Documents/dev/applaudience/foo/src/countries/foo.js:122:23) at process._tickDomainCallback (internal/process/next_tick.js:135:7)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
inikulincommented, Mar 17, 2017

// This is supposed to parse the cookie-string into a Cookie object. Cookie.parse(cookies[0]);

Cookie.parse parses cookie string like the one you set in browser for document.cookie. Meanwhile, jar.getCookiesSync returns array of data structures that represent already parsed cookie. Thus, to access value of the particular cookie you need to scan returned array for required key and use it’s value property. If you need to get string representation of the current cookie jar like the one that returned by browser in Cookies header, then you can use jar.getCookieString method. This interactive example shows both approaches: https://runkit.com/580f9ad527cf550013c20537/58cc1078e26e580014605e9e

0reactions
abhi-carcommented, Aug 10, 2022

Hello Everyone, I have few cookies stored with domain .xxxx.local. when i did `const jar = new CookieJar(); const cookies = jar.getCookiesSync(‘http://xxxx.local’);

console.log('cookies', cookies);
const cookies1 = jar.getCookiesSync('http://app.xxxx.local');

console.log('cookies', cookies1);
const cookies2 = jar.getCookiesSync('http://app1.xxxx.local');

console.log('cookies', cookies2);
const cookies3 = jar.getCookiesSync('.xxxx.local');

console.log('cookies', cookies3);`

I got empty arrays. what is the way to get the cookies.

image
Read more comments on GitHub >

github_iconTop Results From Across the Web

Get cookie from CookieJar by name - python - Stack Overflow
Yes, the __iter__ method will go through each cookie in CookieJar . for cookie in cj: print cookie.name, cookie.value, cookie.domain #etc ...
Read more >
http.cookiejar — Cookie handling for HTTP clients — Python ...
The http.cookiejar module defines classes for automatic handling of HTTP cookies. It is useful for accessing web sites that require small pieces of...
Read more >
CookieJar in axum_extra::extract::cookie - Rust - Docs.rs
This value must be returned from the handler as part of the response for the changes to be propagated. Example. use axum::{ Router,...
Read more >
Using cookies - Postman Learning Center
To retrieve a cookie, use the .get() function. This function takes a URL and the name of the required cookie. It returns the...
Read more >
CookieJar object - Grafana k6
CookieJar ; cookiesForURL(url), Get Object of cookies where the key is the cookie name and the value is an array. ; set(url, name,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found