Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Seek help posting to www-form-urlencoded . . .

by hesco (Deacon)
on Sep 17, 2009 at 06:58 UTC ( [id://795798] : perlquestion . print w/replies, xml ) Need Help??

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

UPDATE:

Thank Mortiz and Ikegami. This issue is now resolved, this code works:

#!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use HTTP::Request::Common qw( POST ); use Data::Dumper; my $ua = LWP::UserAgent->new(); my $url = 'http://lobitos.net/cgi-sys/cgiwrap/jlundell/stv-count.py'; my $ballot_file = '/home/hesco/foo/bar/ballot_file.txt'; my $response = $ua->request(POST $url, Content_Type => 'multipart/form-data', Content => [ datafile => [ $ballot_file ], action => 'Count', ], ); # print Dumper($response->{'_content'}); print Dumper($response->content()); 1;
. . . and my original question:


I'm working to upload a file to a webform with LWP. Following a pointer from Utilitarian, shared in the cb, I read this thread and am working to adapt code from there (although some of it looks like narley and required some work just to compile). Nonetheless it seems I'm on to something here.

And my question, what am I missing here?

My test script reads:

#!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use Data::Dumper; my $ua = LWP::UserAgent->new(); my $url = 'http://lobitos.net/cgi-sys/cgiwrap/jlundell/stv-count.py'; my $ballot_file = '/home/hesco/foo/bar/ballot_file.txt'; my $request = ( # POST => $url, action => 'Count', name => 'datafile', Content_Type => 'multipart/form-data', Content => [ 'file' => [ $ballot_file ]] ); my $response = $ua->post($url,$request); print Dumper($response); 1;
And the form I'm posting against reads:

<form action="/cgi-sys/cgiwrap/jlundell/stv-count.py" enctype="multipa +rt/form-data" method="post"> <p>Specify an election file: <input type="file" name="datafile" size=" +40"><br> then <input type="submit" name="action" value="Count"></p> </form>
My output reads:

hesco@marcus8:~$ perl GP/GPotUS/2009/ETC/lib/upload_test.pl Useless use of a constant in void context at lib/upload_test.pl line 1 +5. Useless use of a constant in void context at lib/upload_test.pl line 1 +5. Useless use of a constant in void context at lib/upload_test.pl line 1 +5. Useless use of a constant in void context at lib/upload_test.pl line 1 +5. Useless use of a constant in void context at lib/upload_test.pl line 1 +5. Useless use of a constant in void context at lib/upload_test.pl line 1 +5. Useless use of a constant in void context at lib/upload_test.pl line 1 +5. $VAR1 = bless( { '_protocol' => 'HTTP/1.1', '_content' => 'no field \'datafile\'', '_rc' => '200', '_headers' => bless( { 'client-date' => 'Thu, 17 Sep 2009 06:24:54 GMT', 'content-type' => 'text/plain', 'connection' => 'close', 'client-response-num' => 1, 'date' => 'Thu, 17 Sep 2009 06:25:35 GMT', 'client-transfer-encoding' => [ 'chunked' ], 'client-peer' => '216.92.168.130:80', 'server' => 'Apache/2.2.13' }, 'HTTP::Headers' ), '_msg' => 'OK', '_request' => bless( { '_content' => 'file=%2Fhome%2Fhesco%2Ffoo%2Fbar%2Fballot_file.txt' +, '_uri' => bless( do{\(my $o = 'http://lobitos.net/cgi-sys/cgiwrap/ +jlundell/stv-count.py')}, 'URI::http' ), '_headers' => bless( { 'user-agent' => 'libwww-perl/5.830', 'content-type' => 'application/x-www-form-urlencoded', 'content-length' => 50 }, 'HTTP::Headers' ), '_method' => 'POST', '_uri_canonical' => $VAR1->{'_request'}{'_uri'} }, 'HTTP::Request' ) }, 'HTTP::Response' ); hesco@marcus8:~$
And here is a sample ballot file to play with:

title=Elect Your Favoprite Ice Cream seats=4 threshold=droop threshold=strict verbose=3 random=37863 candidate=1 Rocky Road candidate=2 Chocolate candidate=3 Strawberry Sherbert candidate=4 Cinnamon Swirl candidate=6 Peanut Butter Fudge candidate=8 Pralines and Cream candidate=9 Cookies and Cream candidate=10 Pecan Sandies candidate=12 Vanilla ala Mode candidate=13 Strawberry Shortcake candidate=14 Popsickles candidate=15 Vanilla candidate=17 Moose Tracks candidate=20 Ice Cream Sandwiches candidate=21 Fudgecycles candidate=22 Frozen Puppies candidate=23 Homemade candidate=24 Truffles candidate=11 None of the above ballots=113 4 3 1 2 6 6 1 4 2 3 1 6 8 4 1 4 3 2 1 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1 6 4 end
if( $lal && $lol ) { $life++; }
if( $insurance->rationing() ) { $people->die(); }

Replies are listed 'Best First'.
Re: Seek help posting to www-form-urlencoded . . .
by moritz (Cardinal) on Sep 17, 2009 at 07:08 UTC

    The warnings point to the right direction.

    my $request = ( # POST => $url, action => 'Count', name => 'datafile', Content_Type => 'multipart/form-data', Content => [ 'file' => [ $ballot_file ]] );

    You put a list (...) in scalar context, which means that $request will get only the last item of that list.

    If mechanize expect an array reference, you have to write [...] around it, if it expects a hash reference it's {...}.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Seek help posting to www-form-urlencoded . . .
by ikegami (Patriarch) on Sep 17, 2009 at 15:29 UTC
    It looks like the syntax of the HTTP::Request::Common's POST function, except you replaced the call to POST with the string POST. And then you commented out that line entirely! The following is closer to what you want
    use HTTP::Request::Common qw( POST ); my $request = POST($url, action => 'Count', name => 'datafile', Content_Type => 'multipart/form-data', Content => [ 'file' => [ $ballot_file ]] );

    But you're not quite there yet. You have content fields outside of Content. Fixed:

    use HTTP::Request::Common qw( POST ); my $request = POST($url, Content_Type => 'multipart/form-data', Content => [ file => [ $ballot_file ], action => 'Count', name => 'datafile', ], );

    The example in the docs is

    POST 'http://www.perl.org/survey.cgi', Content_Type => 'form-data', Content => [ name => 'Gisle Aas', email => 'gisle@aas.no', gender => 'M', born => '1964', init => ["$ENV{HOME}/.profile"], ]

    I don't know how you got from that to what you posted. Or maybe you didn't bother to read the docs...