codemonkey has asked for the wisdom of the Perl Monks concerning the following question:

Ok I have read every archived posting on LWP:UserAgent and HTTP::Cookies and no, I still don't get it. Yes I am probably increadibly lame. I keep missing something about setting cookies. What I am trying to do is post to a remote server which then logs the cookie I send it. I can't send the cookie as a form attribute, so I have to set the cookie to whatever the user's cookie is. The problem is that when I try to set $cookie_jar->add_cookie_header($ENV{HTTP_COOKIE}); It complains greatly about: Can't locate object method "url" via package "<$ENV{HTTP_COOKIE}>" at /usr/local/lib/perl5/site_perl/5.005/HTTP/Cookies.pm line 106. Here is the code:
use LWP::UserAgent; use HTTP::Cookies; use HTTP::Request::Common qw(POST); my $ua = new LWP::UserAgent; my $cookie_jar = new HTTP::Cookies; $cookie_jar->add_cookie_header($ENV{HTTP_COOKIE}); #This line is troub +lesome my $req = POST 'http://foo.com/cgi-bin/log_cookie.cgi', [ data => 'somedata', errors => 'probably' ]; print $ua->request($req)->as_string;

Replies are listed 'Best First'.
Re: LWP and slow learners
by Fastolfe (Vicar) on Jan 30, 2001 at 00:09 UTC
    As the documentation for HTTP::Cookies indicates, the 'add_cookie_header' requires an HTTP::Request object as its argument, and you're passing what appears to be a string.

    When dealing with LWP, don't think of a cookie as being something that you yourself attach to a specific request. Think of a "cookie jar" as a totally separate resource. Separately from your request, you have to add your cookie (via the 'set_cookie' method to HTTP::Cookies) to the cookie jar with the site's information (e.g. foo.com, etc.) and the details/value of the cookie. Then, associate that cookie jar with your LWP::UserAgent object and make your request. Let LWP::UserAgent figure out which cookies it needs to send to the server, based upon the contents of the cookie jar. All you need to do is be sure the cookie jar has the data you expect.

(jcwren) Re: LWP and slow learners
by jcwren (Prior) on Jan 30, 2001 at 00:41 UTC

    If you'd like an example of cookie code in operation, check either ZZamboni's homenode for a link to his chatterbox client, or Homenode Updater (for which I used that chatterbox cookie code as a basis to learn how to use cookies).

    --Chris

    e-mail jcwren