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

POSTing from one script to another

by Anonymous Monk
on May 05, 2004 at 08:25 UTC ( [id://350710]=perlquestion: print w/replies, xml ) Need Help??

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

I've been trying to get a script to post to another script for some time now but to no avail! Sending the LWP POST request to the receiving script and printing out the request/results, I get the following:
POST http://llc.compsys.org/up.cgi Content-Length: 177 Content-Type: multipart/form-data; boundary=xYzZY --xYzZY Content-Disposition: form- +data; name="upload"; filename="success.txt" Content-Length: 36 Content-Type: text/plain IT WORKED!!! 5th May 2004 11:24am --xYzZY-- It didn't work. 501 (Not Implemented) syntax error Content-Type: text/ +plain Client-Date: Wed, 05 May 2004 03:37:10 GMT Client-Warning: Internal re +sponse 501 syntax error 501 syntax error
Both sending and receiving scripts can be on the same server or 2 separate servers, and will generate the same error. I'm a relative newbie to Perl so I desperately need help on this! Any URLs or FAQs I can refer to to get more info on my troubles? Is it a Perl problem or a webserver one? Much appreciated in advance! Below is my POSTer code:
#!/usr/bin/perl use CGI::Carp qw(fatalsToBrowser); use CGI qw(:standard); use lib 'modules'; use LWP::UserAgent 2.031; use HTTP::Request::Common 1.22 qw/POST/; my $ua= LWP::UserAgent->new(); my $url = "http://llc.compsys.org/up.cgi"; my $fullpath_filename = param("upload"); my $filename = lc($fullpath_filename); $filename =~ s/.*[\/\\](.*)/$1/; print (header()); print (start_html( -title => 'Upload' )); if (!$filename) { print ("No file specified.<br>\n"); print qq~ <form action="upload.cgi" method="post" enctype="multipart/form-dat +a"> File upload: <input type="file" name="upload" size="60"><br> <br> <input type="submit" name="Submit" value="Upload"> </form> </body></html> ~; } else { open UPLOADFILE, ">$filename"; binmode(UPLOADFILE); while ( <$fullpath_filename> ){ print UPLOADFILE; } close UPLOADFILE; print "$filename uploaded!<br>"; my $fullpath = $ENV{'DOCUMENT_ROOT'}."/cgi-bin/$filename"; print "FULLPATH = $fullpath<br>"; print "Above file exists!<br><br>" if -e $fullpath; print "Above file does not exist!<br><br>" if !-e $fullpath; my $request=POST $url , Content_Type=>'form-data', Content => [upload=>[$filename]]; print $request->as_string; print "<br><br>"; my $results=$ua->request($request); if($results->is_success){ print "It's good!\n"; print $results->as_string; print "<br><br>"; } else { print "It didn't work.\n"; print $results->as_string; print "<br><br>"; print $results->status_line(); }
This is my receiving code:
#!/usr/bin/perl use CGI qw(:standard); $filename = param("upload"); $filename =~ s/.*[\/\\](.*)/$1/; $upload_filehandle = param("upload"); print (header()); print (start_html( -title => 'Upload' )); if (!$filename) { print ("No file specified.<br>\n"); } else { $filename = lc($filename); open UPLOADFILE, ">$filename"; binmode(UPLOADFILE); while ( <$upload_filehandle> ){ print UPLOADFILE; } close UPLOADFILE; print ("<b>$filename</b> upload successful!<br>\n"); } print ("</BODY>\n</HTML>");

Replies are listed 'Best First'.
Re: POSTing from one script to another
by z3d (Scribe) on May 05, 2004 at 11:45 UTC
    This code runs fine out of apache with one fix - your missing a closing } at the end of your first code block to close the else statement. (I also removed your version requirements for LWP,etc, as I'm not as fancy as you ;) ). If after that it still doesn't work right from the command line, then I would suspect its your command line syntax that's at fault, not the rest of the code.



    "I have never written bad code. There are merely unanticipated features."
      Also, I notice that it worked on my w2k/iis/activestate, but not on the production server, which is freebsd/apache/perl5.6.1... which is so unusual!
Re: POSTing from one script to another
by matija (Priest) on May 05, 2004 at 11:45 UTC
    I think you are trying to test two scripts at the same time.

    That is a bad idea, because when you're debugging something like that, it is very hard to pin down where the problem is.

    First question you should ask yourself is: can an ordinary browser upload a file to my file-receiver module? If it can't, work on the server part until file uploads succeed. Once that is done, you can concentrate on the script you wrote for uploading the files.

    This is a special case of one of the fundamental rules of debugging: "never change more than one thing between test runs".

      My receiving script works fine so I'm not really debugging 2 scripts. I verified the receiving script using a normal HTML multipart/form-data form to post to it. The problem lies with the sending script, which I've been fiddling with for awhile now. I checked the server log: no requests for the 2nd script! Also, no 501 errors. This leads me to conclude that the request isn't even happening. It's dying right after or during the actual LWP/UA request. I read that the header "Client-Warning" is generated by LWP. If this is true, then perhaps there's really something wrong at the sender side? Perhaps a misconfig of the server?
        Ah, concentrating on the uploader, I see that you are specifying Content-type as 'form-data'.

        That is incorrect. It should be 'multipart/form-data'.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-26 08:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found