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

Hello, I am trying to use the post method implemented in perl. The website is : http://enm.lobos.nih.gov/start.html its contents contain:
<FORM ENCTYPE="multipart/form-data" ACTION="prep_job.php" METHOD="POST +"> <INPUT TYPE="HIDDEN" NAME="TASK" VALUE="NEW"> <INPUT TYPE="FILE" NAME="UPLOAD" SIZE=20> <INPUT TYPE="TEXT" NAME="JOBNAME" SIZE=4 MAXLENGTH="4"> <br> <br> <INPUT TYPE="RADIO" NAME="CASE" VALUE="1"> <INPUT TYPE="RADIO" NAME="CASE" VALUE="2"> <INPUT TYPE="RADIO" NAME="CASE" VALUE="3"> <INPUT TYPE="FILE" NAME="UPLOAD2" SIZE=20> <br><BR> <B>E-mail </B> <INPUT TYPE="TEXT" NAME="EMAIL" SIZE=40> <br>< +br> <input type="checkbox" value="true" name="optin"> <INPUT TYPE="submit" VALUE="submit my job">
and I want to post data in my program
#!/usr/bin/perl -w use strict; use LWP::UserAgent; my $browser = LWP::UserAgent->new; my $response = $browser->post( 'http://enm.lobos.nih.gov/start.html', [ "upload"=> ["C:\\protean\\1o1l\\1o1l.pdb"], "jobname"=> "PRT1", "case" => "2", "email" => "email\@gmail.com", "optin" => "true", "upload2"=> ["C:\\protean\\1o1l\\1o1l.pdb"], ], 'Content_Type' => 'form-data', ); die "Error: ", $response->status_line unless $response->is_success; print $response->content; exit;
However when I do this instead of turning a result page like this; http://enm.lobos.nih.gov/success.php?jobno=36439947 it returns the original page as the content. What might be the problem? Does have to do with the code or the website? Thanks

Replies are listed 'Best First'.
Re: Problem Using POST
by grep (Monsignor) on Feb 02, 2008 at 01:00 UTC
    The form you want to post to is found in the <form> tag: prep_job.php

    So you want to change your request to something like.

    my $response = $browser->post( 'http://enm.lobos.nih.gov/prep_job.php', [ "upload"=> ["C:\\protean\\1o1l\\1o1l.pdb"], "jobname"=> "PRT1", "case" => "2", "email" => "email\@gmail.com", "optin" => "true", "upload2"=> ["C:\\protean\\1o1l\\1o1l.pdb"], ], 'Content_Type' => 'form-data', );
    grep
    One dead unjugged rabbit fish later...
      When I do it like the way you say, it says no pdb is supplied (which is that upload input type). Is there something I am doing wrong there? (I will also look at mechanize)
      I also tried this to no use. It still returns "no pdb(the file) uploaded" error.
      #!/usr/bin/perl -w # pl8.pl - query California license plate database use strict; use LWP::UserAgent; use HTTP::Request::Common; use HTTP::Headers; my $browser = LWP::UserAgent->new; my $request = HTTP::Request->new(); my $url="http://enm.lobos.nih.gov/prep_job.php"; my $file="C:/protean/1o1l/1O1L.pdb"; my $response = $browser->request( POST $url, 'Content_Type' => 'form-data', Content=>[ "upload"=> [$file], "jobname"=> "PRT1", "case" => "2", "email" => "sinatureli\@gmail.com", "optin" => "true", "upload2"=> [$file], ], ); die "Error: ", $response->status_line unless $response->is_success; print $response->content; exit;
Re: Problem Using POST
by nikosv (Deacon) on Feb 03, 2008 at 20:06 UTC
    In cases of http applications that need debugging I would suggest the use of a http sniffer.
    It can intercept all communication between the client and the server.
    When in doubt you can actually see what is send back and forth, in this case it can show you how the form data is sent to the server.
      Hmm what library does this http sniffer belong to ? Thanks

        Albeit this is not directly perl related, both the tamperdata and firebug extensions for the firefox browser offer a fairly straight forward "HTTP inspector/sniffer" interface.

        However, you might find it more beneficial to your perl knowledge to use the LWP Post command, or even roll your own proxy along the lines of HTTP::Recorder which can be used to create automated test scripts. ( http://www.perl.com/pub/a/2004/06/04/recorder.html )