in reply to LWP, extract_cookies, etc.

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/)

Replies are listed 'Best First'.
HTTP 302 error now??
by inblosam (Monk) on May 22, 2002 at 15:48 UTC
    Very helpful, but now I have that crazy HTTP 302 error. I had to add use HTTP::Cookies; into the script again because it didn't like not having it.

    Any ideas why I get the error and/or how do I get around it? Also, Ilya you had simple_request on the second post. Did you mean to have just 'request' there? And do I not need "my $request = " on the first "$ua->request(POST..." line? Or the "$ua->cookie_jar" line?

    What I am trying to accomplish is to update my user information on a website that requires a login. So first I need to login and then use the cookie info for posting my user information on a different form. THANKS!

    #!/usr/lib/perl -w use strict; use LWP::UserAgent; use HTTP::Request::Common; # didn't work without this use statement use HTTP::Cookies; my $ua = LWP::UserAgent->new; $ua->cookie_jar(HTTP::Cookies->new(file => 'cookie_jar', autosave =>1) +); $ua->request(POST "http://www.sitedomain.com/login.cfm", { username =>'abcuser', userpass =>'abc123', submit =>'Submit' }); my $request = $ua->request(POST "http://www.sitedomain.com/editinfopag +e.cfm", { firstname =>"MyName", lastname =>"MyLastName", company =>"companyname", address =>"111 East 222 South", city =>"Provo", state =>"UT", zip =>"99999" }); print $request->is_success ? "worked\n" : "failed\n"; # this shows me that I have the following error: # HTTP/1.1 302 (Found) Object Moved... print $request->as_string;
      Very helpful, but now I have that crazy HTTP 302 error.

      The problem with HTTP error is caused by default behaviour of LWP::UserAgent. Sorry I forgot about it when I wrote my version of this script. LWP::UserAgent by default doesn't handles redirects after POST requests automatically. If you have not very old version of LWP::UserAgent you can redifine this behaviour using following sniplet:

      push @{ $ua->requests_redirectable }, 'POST';

      Also, Ilya you had simple_request on the second post. Did you mean to have just 'request' there?

      I'm sorry. I did mean $ua->request instead of $ua->simple_request.

      And do I not need "my $request = " on the first "$ua->request(POST..." line?

      Since you do not use response object from first request there is no need to store it in any variable.

      I have updated script in my first node with all fixes.

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

        IlyaM: You have been great!

        It still doesn't post the data and gives me what seems to be the screen that would show up if someone was not logged in. I used your newest code (just a note, I had to add a ");" on the end of line 12) and it gave me the following in the header of the page returned:
        HTTP/1.1 200 OK Connection: close Date: Wed, 22 May 2002 19:56:15 GMT Server: Microsoft-IIS/5.0 Content-Type: text/html Expires: 0 Client-Date: Wed, 22 May 2002 19:58:47 GMT Client-Response-Num: 1 Page-Completion-Status: Normal Page-Completion-Status: Normal Set-Cookie: CFGLOBALS=HITCOUNT%3D46%23LASTVISIT%3D%7Bts+%272002%2D05%2 +D22+12%3A56%3A16%27%7D%23TIMECREATED%3D%7Bts+%272002%2D05%2D20+23%3A4 +7%3A29%27%7D%23; expires=Sun, 27-Sep-2037 00:00:00 GMT; path=/; Set-Cookie: LASTVISIT=%7Bts+%272002%2D05%2D22+12%3A56%3A16%27%7D; expi +res=Sun, 27-Sep-2037 00:00:00 GMT; path=/; Set-Cookie: SID=99999999999999999999; expires=Fri, 21-Jun-2002 12:56:1 +6 GMT; path=/; Title: Page Title
        The rest of the page comes up with their default signup form (when you are in the member area and not logged in?). Any ideas? It doesn't seem like the cookie is being passed with the second POST request. THANKS!

        Edit by dws for code tags and formatting