Dubcak, Peter wrote:
I need to generate a post http request that sends to a domino server (from a
Solaris box) a bunch of form data including one or more upload files. In
other words, I am trying to mimic the behavior of submission of this type of
form:


<form method="POST" action="http://domino/appname.nsf/M?CreateDocument";
enctype="multipart/form-data">
<input type="text" name="FirstName" value="John">
<input type="text" name="LastName" value="Doe">
<input type="file"
name="%%File7cfd40fb9757495b85256cf0007322bb.$Body.0.7128" value="word.doc">
</form>

To accomplish this, I wrote:

#!/usr/local/bin/perl -w

use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use LWP::UserAgent;
use HTTP::Request::Common;
use HTTP::Headers;

my $q = new CGI;

print "Content-type: text/html\n\n";
my $agent = new LWP::UserAgent;

$result = $agent->request(
POST 'http://domino/appname.nsf/M?CreateDocument',
Content_Type => 'form-data',
Content => [ FirstName => 'John',
LastName => 'Doe',
'%%File7cfd40fb9757495b85256cf0007322bb.$Body.0.7128' =>
["/web/docroot/word.doc"]
]);


print "all is well.\n\n" if $result->is_success;

All data posts fine (including the attached file), except that using the
html method (the first code fragment), the file is readable in MS Word when
downloaded from domino, while using the perl request method, the file comes
out as garbage on the domino side. I noticed that there was a header in the
garbage-d file that read: Content-Type: application/octet-stream which I'm
assuming is the default. I tried to remedy the situation by changing
["/web/docroot/word.doc"] to
["/web/docroot/word.doc","word.doc",'Content-type' => 'application/msword']
in the second code fragment. The transferred file still comes out of domino
as garbage though, although the header in the garbage-d file now reads:
Content-Type: application/msword


I also thought that the wacky domino form field name for the upload file
could have something to do with the problem, but the file does get uploaded
and associated with the correct record. It seems that there may be other
header entries I may need to set in the HTTP::Request::Common object for the
upload field to get the encoding or whatever set right for the MS Word doc.


Btw, both the perl (5.6.0) script and domino server are running on solaris
(SunOS 5.7).

All I can do is offer a script that worked for me to test my upload script (meant to run from command line, so if you run it from CGI, add the content header etc. - I added your name args). Only real diff seems to be the filename arg.

use strict;
use LWP::UserAgent;
use HTTP::Request::Common qw(POST);

my $URL = 'http://localhost/cgi-bin/upload.pl';
my $File = './some.doc';                # local path to file

my $ua = new LWP::UserAgent;
my $req = POST $URL, Content_Type => 'form-data',
  Content => [ FirstName  => 'John', LastName => 'Doe',
  filename => [ $File ] ];

my $res = $ua->request($req) or die "request: $!";
if ($res->is_success) {
        print $res->content();
}

__END__

--
  ,-/-  __      _  _         $Bill Luebkert   ICQ=162126130
 (_/   /  )    // //       DBE Collectibles   Mailto:[EMAIL PROTECTED]
  / ) /--<  o // //      http://dbecoll.tripod.com/ (Free site for Perl)
-/-' /___/_<_</_</_     Castle of Medieval Myth & Magic http://www.todbe.com/


_______________________________________________ Perl-Unix-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to