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

I have a script that needs to send a proper header with two of cookies to a CGI program. I read the HTTP::Cookies documentation, but I don't understand it. My script looks like this:
use warnings; use strict; use LWP::UserAgent; use HTTP::Request; use HTTP::Response; use HTTP::Cookies; use URI::URL; $|++; my $url = url( 'http://www.somedomain.com/cgi-bin/message.cgi' ); my $ua = LWP::UserAgent->new; my $request = HTTP::Request->new( GET => $url ); $request->referer('http://who.wantsta.no/'); my $jar = HTTP::Cookies->new; $jar->set_cookie( 1, 'message', 2 ); # I want to have Set-Cookie: mess +age=2 $jar->set_cookie( 1, 'user', 5 ); # Trying to set a second cookie $jar->add_cookie_header( $request ); my $response = $ua->request( $request ); print $response->as_string;
My debuging shows that the cookies are not being set. What am I doing wrong? Plese help.

Replies are listed 'Best First'.
Re: Send cookie to CGI script
by sutch (Curate) on Dec 16, 2000 at 02:56 UTC
    Examine your cookie jar with
      print $jar->as_string;
    Supplying more arguments to set_cookie does help, i.e.:

      $jar->set_cookie(undef, 'message', 2, '/', '.somedomain.com', 80, 0, 0, 100, 0, {});
      print $jar->as_string;

    Which results in:

    Set-Cookie3: message=2; path="/"; domain=".somedomain.com"; port=80; expires="2000-12-15 21:59:27Z"; version=0

    I have noticed that set_cookie's $domain parameter requires two periods or it will not accept the cookie.

Re: Send cookie to CGI script
by isotope (Deacon) on Dec 16, 2000 at 02:50 UTC
    Your code looks mostly ok, but you're not going to be looking for Set-Cookie in your request... that's what the server would return. You're gonna be sending a Cookie: header. What are you seeing in your debugging?

    --isotope
    http://www.skylab.org/~isotope/