The problem in you code is that you mixing HTTP::Response and HTTP::Request objects. Method call $ua->simple_request returns HTTP::Response object which you assign to variable $request. Until line
$cookie_jar->add_cookie_header($request);
you use this object correctly (though variable name is very misleading). On this line your program crashes because $cookie_jar->add_cookie_header expects HTTP::Request object instead of HTTP::Response. Next line also is not going to work because $ua->request also expects HTTP::Request object.

Well, this is the reasons why it crashes. Anyway I'm not sure what are you trying to achive. Seems that you just want to make two POST requests. Why do you want to handle redirects yourself? LWP can do it itself. Why do you want to handle cookies yourself? LWP again can do it itself. Let me show my version of this code.

#!/usr/lib/perl # be good boy and always do following use strict; use warnings; # you don't have to use other HTTP modules because # LWP::UserAgent uses them itself use LWP::UserAgent; use HTTP::Request::Common; use HTTP::Cookies; my $ua = LWP::UserAgent->new; # this method call creates cookie jar object and tells # LWP::UserAgent object to use it automatically. You do not # have to call extract_cookies or add_cookie_headers # yourself $ua->cookie_jar(HTTP::Cookies->new(file => 'cookie_jar', autosave =>1) +; # configure LWP::UserAgent to follow redirects after POST push @{ $ua->requests_redirectable }, 'POST'; # first POST request - no need to handle redirects youself. # Let LWP handle them automatically. This is why I use # 'request' instead of 'simple_request'. $ua->request(POST "http://www.sitedomain.com/logi +n.cfm", { username =>'abcuser', userpass =>'abc123', submit =>'Submit' }); # Second POST request - same as in your code my $request = $ua->request(POST "http://www.sitedomain.com/page.cfm", { firstname =>"Michael", lastname =>"Jensen", company =>"companyname", address =>"111 East 222 South", city =>"Provo", state =>"UT", zip =>"99999" }); print $request->is_success ? "worked\n" : "failed\n";
BTW are you trying to write automated test for login form? Consider using web testing modules like HTTP::WebTest or HTTP::Monkeywrench.

P.S. And of course let's repeat it again: Use strict and warnings!

Update: s/simple_request/request/, added code to turn on handling of redirects after POST in LWP::UserAgent, added use HTTP::Cookies.

Update: added missing ';'

--
Ilya Martynov (http://martynov.org/)


In reply to Re: LWP, extract_cookies, etc. by IlyaM
in thread LWP, extract_cookies, etc. by inblosam

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.