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

I am trying to use LWP to simulate the submission of a form. However, during the submission of the form, I also want to simulate as if the browser has some cookie set already set. So far I tried a ton of things and nothing really works except this other hack I did which I don't like. If someone has a better solution that they can share, I would really appreciated it.
---- This is what I tried and doesn't work ----- use LWP::UserAgent; use HTML::Form; use LWP::Debug qw(+); use HTTP::Cookies; use Data::Dumper; # This site if redirected from another login page where the user # inputs a specific user/password or if the user and password is passe +d in , a specific cookie is sets # Assessing this page directly will have a default cookie set $site = "http://some_site_with_a_form.com"; # Creating the LWP user agent $ua = LWP::UserAgent->new; $ua->agent("Mozilla/5.0"); # Creating cookie container $cookie_jar = HTTP::Cookies->new(); $cookie_jar->set_cookie(1, "foo", "bar", "/", "http://some_site_with_a +_form.com", "9000", 0, 0, 1000, 0); # tying the cookie container with the user agent $ua->cookie_jar($cookie_jar); # get the form from this site @forms = $ua->post($site); $form1 = $forms[0]; # only interested in the first form # and I can do something here to replace some values # from the form 1 but not needed for what I'm trying to show # $ua->add_cookie_header($form1.click) my $res = $ua->request($form1.click); # shows the cookie is set to default, which tells me # whatever I tried to do is useless print Dumper($res); -- It seems to work if I go into the site with the -- userid/password first which then sets the correct -- cookie use LWP::UserAgent; use HTML::Form; use LWP::Debug qw(+); use HTTP::Cookies; use Data::Dumper; # This site if redirected from another login page where the user # inputs a specific user/password or if the user and password is passe +d in , a specific cookie is sets # Assessing this page directly will have a default cookie set $site = "http://some_site_with_a_form.com"; # Creating the LWP user agent $ua = LWP::UserAgent->new; $ua->agent("Mozilla/5.0"); # Creating cookie container $cookie_jar = HTTP::Cookies->new(); # not needing this, just create an empty cookie container # $cookie_jar->set_cookie(1, "foo", "bar", "/", # "http://some_site_wi +th_a_form.com", "9000", 0, 0, 1000, 0); # tying the cookie container with the user agent $ua->cookie_jar($cookie_jar); # What I have to do, pretending to go into the page with # with a password. But one can see this is lame and # very bad form to expose password $res = $ua->get($site . "?user=admin&password=adminpasswd"); ## @forms = $ua->post($site); $form1 = $forms[0]; # only interested in the first form # and I do something to replace some values from the form 1 # $ua->add_cookie_header($form1.click) my $res = $ua->request($form1.click); # shows the cookie is set for the specific user "admin print Dumper($res);

Replies are listed 'Best First'.
Re: LWP Cookies with HTML::Form
by targetsmart (Curate) on Apr 15, 2009 at 06:03 UTC
    will this node DIfference between LWP and WWW::Mechanize help you?

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.