in reply to What is a Cookie Jar?

cookie_jar is magic. When a user agent (e.g. your browser or your LWP::UserAgent program) is sent cookies, the site expects them echoed back in subsequent requests. However, a cookie can expire after a set amount of time, different sites will send different cookies - and expect only the ones they sent back. In fact different scripts at the same site will send different coookies and expect the right one sent back.

cookie_jar automates all this, including possibly saving cookies to a file for reading on subsequent execution.

# UNTESTED SNIPPET use LWP::UserAgent; $ua = new LWP::UserAgent; use HTTP::Cookies; $cookie_jar = HTTP::Cookies->new; $init_url = 'http://www.domain.com/cgi-bin/whatever.pl?X'; $request = new HTTP::Request('GET', $init_url); $cookie_jar->extract_cookies($response); # later.... $request = new HTTP::Request('GET', 'http://www.domain.com/cgi-bin/wha +tever.pl?Y'; $cookie_jar->add_cookie_header($request); $response = $ua->simple_request($request);
Just extract from responses and add to requests - the cookie_jar does the heavy lifting.

--Bob Niederman, http://bob-n.com

Replies are listed 'Best First'.
Re: Re: What is a Cookie Jar?
by Anonymous Monk on Jul 04, 2003 at 17:25 UTC
    Is there a reason you say "$request = new HTTP::Request('GET', $init_url);", but then on the next line the variable is $reponse instead of $request?

      Yes. I''m a space cadet.

      I was cutting and pating and forgot:

      $response = $ua->request($request);
      Also, after reading the lwpcookbook referenced in another post here, it appears I'm still doing more work than needed. Apparently, just attaching the cookie_jar to the UserAgent object does the job, thouhg I haven't actually tested this.

      update: I probably had to do the extract and add because I was using simple_request, due to doing other things (referer, other headers in redirected requests.) I expect that folks using the the normal $ua->request methind can just do a $ua->cookie_jar(options..) and forget about it.

      --Bob Niederman, http://bob-n.com