Urgent Help needed with Post method file upload
Urgent Help needed with Post method file upload
am 13.08.2005 19:53:33 von Eric
------=_NextPart_000_0018_01C59FFD.A1C2D610
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
I'm attempting to post a file in a multipart/form-data post using =
LWP::UserAgent and the following command string:
my $response =3D $ua->request(POST $baseurl, Content_Type =3D> =
'form-data', Content =3D> [ request_params =3D> $task_add_postkey, =
datafile =3D> [$task_add_datakey] ]);
Where $task_add_postkey is a set of key value pairs of form data, and =
$task_add_datakey is the full path file name of a file containing binary =
data. The post seems to work fine with the exception of the 48 byte file =
I am sending, is received as 61 bytes on the server. I'm assuming it has =
something to do with how the file is being parsed.=20
Is there a better way to put together a multipart post? Am I missing =
something obvious? Is there a way to control the file parser so that the =
data is handled as raw binary data?=20
Any ideas????
Thanks in advance..
Eric
------=_NextPart_000_0018_01C59FFD.A1C2D610--
Re: Urgent Help needed with Post method file upload
am 13.08.2005 20:05:26 von gisle
"Eric Langheinrich" writes:
> I'm attempting to post a file in a multipart/form-data post using
> LWP::UserAgent and the following command string:
>
> my $response = $ua->request(POST $baseurl, Content_Type => 'form-data', Content => [ request_params => $task_add_postkey, datafile => [$task_add_datakey] ]);
>
>
> Where $task_add_postkey is a set of key value pairs of form data,
> and $task_add_datakey is the full path file name of a file
> containing binary data. The post seems to work fine with the
> exception of the 48 byte file I am sending, is received as 61 bytes
> on the server. I'm assuming it has something to do with how the file
> is being parsed.
Are you able to inspect the file received on the server side to figure
out what is actually different?
> Is there a better way to put together a multipart post? Am I missing
> something obvious? Is there a way to control the file parser so that
> the data is handled as raw binary data?
The file should already be included as binary. There is no file
parser involved.
> Any ideas????
Try to print the request that POST() returns above to see if you can
spot what is wrong.
$req = POST($baseurl, ....);
print $req->as_string;
$res = $ua->request($req);
...
Regards,
Gisle
Re: Urgent Help needed with Post method file upload
am 13.08.2005 20:13:38 von Eric
Okay, so in the useragent, I pass the filename as an array reference
[$task_add_datakey] what actually gets the contents of the file and posts
them?
Is there another way to do it? Am I better off opening a raw socket and
writing the headers and the data myself? My concern is that to get the raw
binary to write properly in the first place, I had to run binmode FILEHANDLE
":raw" on the file handle that I was writing to. From what I've read, I
would need to do that on the read of the file contents as well in order to
properly get the data. Is the library setup to handle raw binary data?
----- Original Message -----
From: "Gisle Aas"
To: "Eric Langheinrich"
Cc:
Sent: Saturday, August 13, 2005 12:05 PM
Subject: Re: Urgent Help needed with Post method file upload
> "Eric Langheinrich" writes:
>
>> I'm attempting to post a file in a multipart/form-data post using
>> LWP::UserAgent and the following command string:
>>
>> my $response = $ua->request(POST $baseurl, Content_Type => 'form-data',
>> Content => [ request_params => $task_add_postkey, datafile =>
>> [$task_add_datakey] ]);
>>
>>
>> Where $task_add_postkey is a set of key value pairs of form data,
>> and $task_add_datakey is the full path file name of a file
>> containing binary data. The post seems to work fine with the
>> exception of the 48 byte file I am sending, is received as 61 bytes
>> on the server. I'm assuming it has something to do with how the file
>> is being parsed.
>
> Are you able to inspect the file received on the server side to figure
> out what is actually different?
>
>> Is there a better way to put together a multipart post? Am I missing
>> something obvious? Is there a way to control the file parser so that
>> the data is handled as raw binary data?
>
> The file should already be included as binary. There is no file
> parser involved.
>
>> Any ideas????
>
> Try to print the request that POST() returns above to see if you can
> spot what is wrong.
>
> $req = POST($baseurl, ....);
> print $req->as_string;
>
> $res = $ua->request($req);
> ...
>
> Regards,
> Gisle
>
>
Re: Urgent Help needed with Post method file upload
am 13.08.2005 20:52:48 von Eric
> Are you able to inspect the file received on the server side to figure
> out what is actually different?
I'm not able to inspect the file on the host. I get back and XML string with
a result.
> Try to print the request that POST() returns above to see if you can
> spot what is wrong.
>
> $req = POST($baseurl, ....);
> print $req->as_string;
>
> $res = $ua->request($req);
The response from the server is basic header information and an XML string.
I have the following to receive the response:
if ($response->is_success) {
print $response->as_string;
print $response->content;
my $task_add_response = $response->content;
$_[1] = $task_add_response;
} else {
die_nice($response->status_line, "\n");
}
Thank you,
Eric
Re: Urgent Help needed with Post method file upload
am 14.08.2005 00:40:37 von Eric
I reread the docs and saw this:
[ $file, $filename, Header => Value... ]
[ undef, $filename, Header => Value,..., Content => $content ]The first
value in the array ($file) is the name of a file to open. This file will be
read and its content placed in the request. The routine will croak if the
file can't be opened. Use an undef as $file value if you want to specify the
content directly with a Content header. The $filename is the filename to
report in the request. If this value is undefined, then the basename of the
$file will be used. You can specify an empty string as $filename if you want
to suppress sending the filename when you provide a $file value.
And updated my post request to look like this:
my $response = $ua->request(POST $baseurl, Content_Type => 'form-data',
Content => [ request_params => $task_add_postkey, datafile => [ undef,
'datafile', Content-Type => 'application/octet-stream', Content =>
$filedata ] ]);
When I run the script, I get:
Bareword "Content" not allowed while "strict subs" in use at
regcompliance.pl line 438
Any ideas?
Thank you,
Eric
Re: Urgent Help needed with Post method file upload
am 14.08.2005 01:11:05 von philippe.bruhat
Le samedi 13 ao=FBt 2005 =E0 16:40, Eric Langheinrich =E9crivait:
> I reread the docs and saw this:
>=20
>
>=20
> And updated my post request to look like this:
>=20
> my $response =3D $ua->request(POST $baseurl, Content_Type =3D> 'form-da=
ta',=20
> Content =3D> [ request_params =3D> $task_add_postkey, datafile =3D> [ u=
ndef,=20
> 'datafile', Content-Type =3D> 'application/octet-stream', Content =3D>=20
> $filedata ] ]);
>=20
>=20
> When I run the script, I get:
>=20
> Bareword "Content" not allowed while "strict subs" in use at=20
> regcompliance.pl line 438
>=20
> Any ideas?
s/Content-Type/Content_Type/
--=20
Philippe "BooK" Bruhat
In the contest between simplicity and silence, silence hasn't got a pray=
er.
(Moral from Groo The Wanderer #15 (Ep=
ic))
Re: Urgent Help needed with Post method file upload
am 14.08.2005 17:35:55 von gisle
"Eric Langheinrich" writes:
> Okay, so in the useragent, I pass the filename as an array reference
> [$task_add_datakey] what actually gets the contents of the file and
> posts them?
The form_data() function in the HTTP::Request::Common module reads the
content.
> Is there another way to do it?
I would recommend that you first figure out what the problem is before
you start looking for another way. As I said, the file data is
already read in binary mode, so that's certainly not the problem.
> Am I better off opening a raw socket
> and writing the headers and the data myself? My concern is that to get
> the raw binary to write properly in the first place, I had to run
> binmode FILEHANDLE ":raw" on the file handle that I was writing
> to. From what I've read, I would need to do that on the read of the
> file contents as well in order to properly get the data. Is the
> library setup to handle raw binary data?
Regards,
Gisle
Re: Urgent Help needed with Post method file upload
am 14.08.2005 17:39:41 von gisle
"Eric Langheinrich" writes:
> I'm not able to inspect the file on the host. I get back and XML
> string with a result.
>
> > Try to print the request that POST() returns above to see if you can
> > spot what is wrong.
> >
> > $req = POST($baseurl, ....);
> > print $req->as_string;
> >
> > $res = $ua->request($req);
>
> The response from the server is basic header information and an XML
> string. I have the following to receive the response:
What I suggested above was to inspect the "request" before it is sent,
since you seemed to think that it was not set up properly.
Regards,
Gisle
Re: Urgent Help needed with Post method file upload
am 14.08.2005 17:48:05 von Eric
> What I suggested above was to inspect the "request" before it is sent,
> since you seemed to think that it was not set up properly.
At this point, I'm pretty sure the request is perfect! Thank you for the
help. I taking that function out of the overall script and running it
byitself, seems to work fine. The problem must be all mine, although for the
life of me I can't seem to figure out what it would be. When I run the
function in it's own script not as a function, the file size appears to
transmit correctly. When the function as a function within the bigger script
the size is always wrong. I've done character by character comparisons
between the function and it's stand alone mode and can't seem to find any
real difference. Really quite baffling at this point.
Thank you again for all of the help. You definitely pointed me in the right
direction.