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

Hi all, I have a task at hand , that of uplaoding file from server to another server and also pass 6 paramteres along with the file.

Basically, I have to replicate the functionality provided by CFHTTP tag of COLD FUSION.

I used LWP::UserAgent for the purpose. My Program is given below.

#!usr/local/bin/perl use LWP::Simple; use Data::Dumper; use LWP::UserAgent; use HTTP::Request::Common qw/POST/; LWP::Debug::level('+') ; push(@INC, '/opt/customer/apps/sun-83/nes363/suitespot/cgi-bin/lib/LWP +/') ; my $ua= LWP::UserAgent->new; my $file="/home/hewebadm/nilay/test1.txt" ; my $filename="323272-1980-test1.txt" ; open(READFILE, "<$file") ; @data=<READFILE> ; # Have to post the file and parameters at this URL # my $url="http://downloaddev.pearsoncmg.com/extmanage/extpost.php" ; # I Have hard coded the parameters for now# $request = POST $url, Content_Type => 'application/x-www-form-urlencoded', Content => [ action => "insert", file => "@data",#have tried with file=>$file filename => "$filename", parentisbn => "0131406701", linktext => "test1.txt",#present in curr dir type => "Presentations", imprint =>"ph" , ] ; my $results=$ua->request($request ); print Dumper($results); if($results->is_success){ print $results->as_string() ; print $results->status_line(); }
On executing this program, I am supposed to get back a URL from the site : http://downloaddev.pearsoncmg.com/extmanage/extpost.php

But I am getting : ERROR:
instead. Here is the output of the program:

LWP::UserAgent::new: () LWP::UserAgent::request: () LWP::UserAgent::send_request: POST http://downloaddev.pearsoncmg.com/extmanage/extpost.php LWP::UserAgent::_need_proxy: Not proxied LWP::Protocol::http::request: () LWP::Protocol::collect: read 12 bytes LWP::UserAgent::request: Simple response: OK $VAR1 = bless( { '_protocol' => 'HTTP/1.1', '_content' => 'ERROR: ', '_rc' => '200', '_headers' => bless( { 'connection' => 'close', 'x-powered-by' => 'PHP/4.2.2', + 'client-response-num' => 1, 'date' => 'Sat, 31 Jan 2004 0: +57:02 GMT', 'client-peer' =>'209.202.161.1 +74:80', 'client-date' => 'Sat, 31 Jan +200410:57:04 GMT', 'content-type' => 'text/html', + 'server' => 'Apache/1.3.20 (Un +ix) PHP/4.2.2', 'client-transfer-encoding' => 'chunked' }, 'HTTP::Headers' ), '_msg' => 'OK', '_request' => bless( { '_content' => 'action=insert&file=abcsssss%0A+sad%0A+asd%0A+asd%0A+asd%0A+asd%0A+asd +a%0A+s das%0A+dasd%0A+fsdf%0A+sdf%0A+sdf%0A+sdf%0A+sdfa%0A+dfasd%0A+fas%0A+df +as%0A+ dfs%0A+df%0A+sdf%0A+f%0A&filename=323272-1980-test1.txt&parentisbn=013 +140670 1&linktext=test1.txt&type=Presentations&imprint=ph', '_uri' => bless( do{\(my $o = 'http://downloaddev.pearsoncmg.com/extmanage/extpost.php')}, 'URI::htt +p' ), '_headers' => bless( { 'user-a +gent' => 'libwww-perl/5.76', 'content-type' => 'application/x-www-form-urlencoded', 'content-length' => 339 }, 'HTTP::Headers' ), '_method' => 'POST' }, 'HTTP::Request' ) }, 'HTTP::Response' ); HTTP/1.1 200 OK Connection: close Date: Sat, 31 Jan 2004 10:57:02 GMT Server: Apache/1.3.20 (Unix) PHP/4.2.2 Content-Type: text/html Client-Date: Sat, 31 Jan 2004 10:57:04 GMT Client-Peer: 209.202.161.174:80 Client-Response-Num: 1 Client-Transfer-Encoding: chunked X-Powered-By: PHP/4.2.2 ERROR: 200 OK ERROR:


I want to know, if I am able to talk to the php program , (since if I
do not
pass the paramnteres, I am prompted with the error of missing
paramteres.)
what is wrong with this piece of code ?
And I have no access to the php program.
And I can't suspect the php program to be wrong as the same php program
when called thru Cold fusion program is able to upload the file
successfully. NOTE : I have tried passing the file name as well file data in the line
: file => "@data",

Please guide me.
Thanks

20040209 Edit by Corion: Added code tags around the error message

Replies are listed 'Best First'.
Re: Problem in posting using LWP::UserAgent
by hossman (Prior) on Feb 09, 2004 at 07:02 UTC

    The "ERROR" you are seeing is being printed by the php script ... presumably because it doesn't like your input. the HTTP status code itself is a 200, indicating that everything went fine at the communication layer.

    As for your problem, if you take closer look at the form you are trying to fake out you might notice that it is using "multipart/form-data" as the encoding type ... that's very important to remember.

    If you search arround for "multipart/form-data", you'll find this Q&A which quotes from important parts of the docs for the "POST" method that you should re-read to understand how to indicate (to both the POST method, and the server on the other end) that you want do do a "file upload"

      By Modifying, file=>"$file" to file=>"$file" I was able to upload files of size 1081465 bytes. But the upload fails when I try for bigger files like 2MB or more... What do I need to set to be able to upload bigger files ? Thanks.
        1. Are you sure the server isn't rejecting files bigger then 2MB? ... ie, can you send a file that big with your browser?
        2. Did you read *all* of the docs for the POST method ... all the way down to the section that talks about the <code$DYNAMIC_FILE_UPLOAD</code>
Re: Problem in posting using LWP::UserAgent
by Abigail-II (Bishop) on Feb 09, 2004 at 10:51 UTC
    Note that you have an open in your program whose return value you don't check. Perhaps the open failed, who knows?
    file => "@data"
    Are you sure you want this? You do know this introduces a space at the beginning of each line, except the first line, don't you?

    Abigail