cgi post w/file upload

al (alewis@es-inc.com)
Wed, 3 Nov 1999 05:29:33 -0800


I am trying to post a file to a cgi.  i am having difficulty combining the 
file content with variables i need to set to activate the cgi.  without the 
filename the cgi complains and this way it errors, indicating that the file 
content was not what it expected meaning it it prepended the vars to the 
begining of the file.

thank you,
al


use HTTP::Request;
use LWP::UserAgent;
use URI::Escape;

# -- Open the file containing the LM data to post.
open(LMDATA, "$ARGV[0]") || die "Could not open $ARGV[0].";

# -- Read the data, build the URL-escaped string to POST.
# -- Each line is of the form attribute=value.
$postString = "";
$postString = $postString . "filename=file2" . "&";
$postString = $postString . "DEBUG=1" . "&";
while (<LMDATA>) {
        chomp;
        $postString = $postString . uri_escape($_) . "&";
        $DEBUG && print ("postString = $postString\n");
}
close(LMDATA);

# -- There will be an extra & at the end; get rid of it.
chop($postString);

$request = new HTTP::Request 'POST', $ARGV[1];
$request->header('Content-type' => 'application/x-www-form-urlencoded');
$request->header('Content-length' => length($postString));
$request->content($postString);
$
ua = new LWP::UserAgent;
$response = $ua->request($request);

if ($response->is_success() && !$response->content()) {
        $DEBUG && print "success\n";
} else {
        $DEBUG && print "unsuccessful\n";
        $message = "LM barfed.\nresponse = " . $response->as_string() . 
"\ncontent = " . $response->content . "\n";
        die $message
}