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

How do I set a cookie to be passed in with my www::mechanize request? I have set my cookies, but it seams that they are not getting added to the header. Time is running out too, I have to have this ready for a client monday, any help would be very appreciated! Heres my code.
#!/usr/bin/perl -w use CGI::Carp qw(fatalsToBrowser); use strict; use WWW::Mechanize; use HTTP::Cookies; use LWP 5.64; use LWP::Debug qw(+); print "Content-type: text/html\n\n"; # creating a new cookie jar my $mech = WWW::Mechanize->new(); my $url = 'http://www.kbb.com/KBB/UsedCars/2007_Acura_MDX.aspx'; $mech->cookie_jar(HTTP::Cookies->new()); $mech->cookie_jar->set_cookie(-name=>'PersistentZipCode', -value=>'15213', -host=>'www.kbb.com', -expires=>'Thu, 04 Dec 2008 18:23:16 GMT', -path=>'/' -secure=>'false'); $mech->cookie_jar->set_cookie(-name=>'ZipCode', -value=>'15213', -host=>'www.kbb.com', -expires=>'+2h', -path=>'/', -secure=>'false'); # build a request object my $request = $mech->request->(GET => $url ); $mech->cookie_jar->add_cookie_header($request); $mech->get($url); print $mech->content();

Replies are listed 'Best First'.
Re: setting cookies www::mechanize
by moklevat (Priest) on Dec 09, 2007 at 03:24 UTC
    I don't know if this will solve your problem, but I notice that you are missing a comma after the first -path=>'/'.
      No, but thank you :D didn't notice that! Its amazing it didn't throw an error.

        A quick pass through B::Deparse explains what's happening:

        perl -MO=Deparse,-p < 1 > 17:18:46 $mech->cookie_jar->set_cookie(-name=>'PersistentZipCode', -value=>'15213', -host=>'www.kbb.com', -expires=>'Thu, 04 Dec 2008 18:23:16 GMT', -path=>'/' -secure=>'false'); $mech->cookie_jar->set_cookie((-'name'), 'PersistentZipCode', (-'value +'), '15213', (-'host'), 'www.kbb.com', (-'expires'), 'Thu, 04 Dec 200 +8 18:23:16 GMT', (-'path'), ('/' - 'secure'), 'false');

        I couldn't explain exactly why it's picked this way to parse, but you can see what's happened and why it's not throwing a syntax error though.

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.