Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

HTTP 302 error now??

by inblosam (Monk)
on May 22, 2002 at 15:48 UTC ( [id://168490]=note: print w/replies, xml ) Need Help??


in reply to Re: LWP, extract_cookies, etc.
in thread LWP, extract_cookies, etc.

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;

Replies are listed 'Best First'.
Re: HTTP 302 error now??
by IlyaM (Parson) on May 22, 2002 at 16:10 UTC
    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

        It is hard to say what is wrong. You should try to turn on debug output in LWP::UserAgent. See LWP::Debug. For example if you add following code into your script it will turn on all debug prints inside LWP::UserAgent.
        use use LWP::Debug qw(+);
        Particullary you will be able to see all redirects and all headers. It will show you if you do recieve cookie in response for first request and if it sent back later in second request.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://168490]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2024-04-20 06:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found