Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Submitting a form

by muradkhan (Initiate)
on Feb 26, 2004 at 22:44 UTC ( [id://332126]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all: I could use some help in submitting a form using perl. Here is what the HTML page and my code look like:
<script language="JavaScript"> function submitScreen(actionStr) { document.verify.action.value = actionStr return true; } </script> <tr><td colspan="2" align="center"> <form name="verify" method="post" action="../download/download.asp"> <input type="hidden" name="action" value="abort"> <input type="submit" name="Continue" value="Yes" onClick=submitScreen( +"continue")> <input type="Submit" name="Abort" value="No" ></td> </tr>
Here is what my perl code so far looks like, but I'm not getting the page I want.
use HTTP::Request::Common qw(POST); use LWP::UserAgent; $ua = LWP::UserAgent->new(); my $req = POST 'http://companyname.com/Customer/support/Patch/releases +/ClientDownload/download/download.asp', [continue => 'Yes']; #$req->content_type('application/x-www-form-urlencoded'); $req->content('match=www&errors=0&action=abort&continue=Yes'); my $res = $ua->request($req); if ($res->is_success) { print $res->content; if ($res->content =~ m/no software is available/) { print "Did not get desired page\n"; } } else { print "Bad luck this time\n";
If my code was working correctly, the page presented by the server would have a set of links to files available for downloading. My intent is to monitor this page and start a download followed by an automated install of the software, followed by an invocation of my automated tests. AdThanksVance

Replies are listed 'Best First'.
Re: Submitting a form
by leira (Monk) on Feb 27, 2004 at 00:01 UTC
    Also, this might also be a little bit more to set up than you're looking for, but HTTP::Recorder can record your actions when you interact with the web site normally (through your browser), and produce a WWW::Mechanize script.

    Even if you're planning to use a plain LWP::UserAgent rather than Mech, the script that's produced will tell you what the values are being set to as they get sent back to the web site.

    If you have any problems with HTTP::Recorder, feel free to /msg me.

    Linda

Re: Submitting a form
by leira (Monk) on Feb 26, 2004 at 23:13 UTC
    This isn't a perl thing, but it looks like the JavaScript on that page sets the "action" field to "continue" when you click the Continue button, and you're submitting with the field "action" set to "abort". Could this be the problem?

    Linda

      Leira: I'm not quite sure about what I'm doing, but the Nutshell book on HTML 4.0 suggests that hidden fields may be used to manage user/server interactions. Since I don't know what download.asp is expecting, I was sending this value back just in case it was looking for it. I've tried it both ways, and it still don't work. I suppose I should find the source for download.asp (its company internal, should not be hard to do) and figure out what the heck its looking for. In the mean time, would it not be likely that when submitting a form to download.asp that we might be required to identify, by name, the form being submitted?
Re: Submitting a form
by saintmike (Vicar) on Feb 26, 2004 at 23:07 UTC
    Looks like you're using continue=Yes in your request but the HTML form will use Continue=Yes (uppercase). Also, why are you using both POST and content()?
    POST 'http://somewhere/foo', [action => "abort", Continue => "Yes"]);
    should suffice.
      SaintMike, thank you for responding. the HTML I've displayed is what is coming back from the server when I get past the login page. I'm trying to accomplish in perl what my mouse and fingers do when I click the 'Yes' button. I read somewhere that in calling the Javascript as I have it, the parameter "continue" gets created (lower case). what does this mean ?
      [action => "abort", Continue => "Yes"]
      I'm guessing it's an assignment. The Perl Cookbook seems to use single quotes. Does it matter. Murad
        Just checked, if you press the "yes" button, the browser sends
        * action o continue * Continue o Yes

        and if you're pressing "no", you get
        * action o continue * Abort o No

        So, in your code, you need to say
        POST 'http://companyname.com/Customer/support/Patch/releases /ClientDownload/download/download.asp', [action => "continue", Continue => "Yes"];

        to create a HTTP::Request object simulating the "Yes". Check out the documentation on HTTP::Request::Common -- the POST function expects a URL and a reference to an array containing key-value pairs of CGI parameters you want to pass.

        If you want to figure out what a browser sends to a server, there's a simple trick: Put a cgi-dump script like

        use CGI qw/:standard/; my($self) = CGI::self_or_CGI(); print header(), start_html('-title' => "CGI Dump", '-BGCOLOR' => "white"), h2("Query Parameters:"), CGI::as_string(), h2("Environment:"), (map { p("$_ => $ENV{$_}") } sort keys %ENV), end_html();

        into a cgi-bin directory of a web server you have control over (it can even be on your local machine). Then, save the HTML of the form you're testing to a local file, and replace its action-URL by the URL to the CGI script on your server (e.g. http://localhost/cgi-bin/dump.cgi). If you then load the local HTML form file into the browser (File->OpenFile) and click on the submit button, the browser will contact your server with the dump-script above and show which parameters have actually been submitted -- this way, you don't need to worry about JavaScript, because the browser takes care of it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-16 05:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found