can LWP handle this?

L

Larry

Hi,

I would like to know how to go about using the following code to send
some data to a web server using POST method:

#!/perl

use LWP::UserAgent;

my $ua = LWP::UserAgent->new;
$ua->agent("Naiocast/1.8 (Stable Version)");

my $req = HTTP::Request->new(POST =>
'http://127.0.0.1:8000/cgi-bin/recv.cgi');

$req->content_type('application/octet-stream');

my $res = $ua->request($req);

print $res->as_string;

__END__;

I'm not going to send form data. Instead, I'm sending raw binary data
which I'm not aquainted with the size, so I may as well set a long
content-length

I'm getting binary data from a local sysread:

while ( sysread($sock, $buf, BUFSIZE) )
{
# ...send $buf via POST...(let LWP handle it)
}

Can it atually be done?

thanks
 
L

Larry

Larry said:
I'm not going to send form data. Instead, I'm sending raw binary data
which I'm not aquainted with the size, so I may as well set a long
content-length

ok, I sorted out something like the following:

my $ua = LWP::UserAgent->new;
$ua->agent("Naiocast/1.8 (Stable Version)");

my $req = HTTP::Request->new(POST =>
'http://127.0.0.1:8000/cgi-bin/recv.cgi');

$req->content_type('application/octet-stream');
$req->content_length(2048000000);

$req->content(sub {
my $buf;
sysread($fh1, $buf, 2048);
return $buf;
} );

my $res = $ua->request($req);

print $res->as_string;
 
P

Peter J. Holzer

I'm not going to send form data. Instead, I'm sending raw binary data
which I'm not aquainted with the size, so I may as well set a long
content-length
[...]
$req->content_length(2048000000);

$req->content(sub {
my $buf;
sysread($fh1, $buf, 2048);
return $buf;
} );

If this works with your server, that's fine, but I don't think you can
rely on this. If you send less than 2048000000 bytes, a server might (or
even should) conclude that something went wrong at the client end and
return an error.

hp
 
L

Larry

Peter J. Holzer said:
If this works with your server, that's fine, but I don't think you can
rely on this. If you send less than 2048000000 bytes, a server might (or
even should) conclude that something went wrong at the client end and
return an error.

Thank you for pointing that out. Actually I'm sending endless data and
if I try "transfer chunked" my server will fire a "411 Length Required"
error!
 
L

Larry

Larry said:
ok, I sorted out something like the following:

I have found I cannot send an header data before the rwa data:

I cannot do this: # It won't send "Hello World!" at all

$req->content("Hello World!");

$req->content(sub {
read($fh1, my $buf, 2048);
return $buf;
} );


nor this: # It will send only "Hello world!" for ever, like it weren't
aquainted with $_hsent = 1;

$_hsent = 0;

$req->content(sub {
if $_hsent == 0
{
return "Hello World!";
$_hsent = 1;
} else {
read($fh1, my $buf, 2048);
return $buf;
}
} );

any idea?

thanks
 
C

C.DeRykus

I have found I cannot send an header data before the rwa data:

I cannot do this: # It won't send "Hello World!" at all

$req->content("Hello World!");

$req->content(sub {
read($fh1, my $buf, 2048);
return $buf;
} );

nor this: # It will send only "Hello world!" for ever, like it weren't
aquainted with $_hsent = 1;

$_hsent = 0;

$req->content(sub {
if $_hsent == 0
{
return "Hello World!";
$_hsent = 1;
} else {
read($fh1, my $buf, 2048);
return $buf;
}
} );

any idea?

Untested but a possible simpler alternative:

$hello = "Hello World!";
$len = length $hello;

$req->add_content($hello);
while ( $num = read($fh1, my $buf, 2048) ) {
$len += $num;
$request->add_content($buf);
}
die "read error: $!" unless defined $num;
$request->header('content-length' => $len);
....
 
P

Peter J. Holzer

Thank you for pointing that out. Actually I'm sending endless data and
if I try "transfer chunked" my server will fire a "411 Length Required"
error!

In this case you will at some point have sent 2048000000 bytes. Then the
server will consider the input complete and send a reply.

Do you use HTTP/1.0 or HTTP/1.1? In HTTP/1.1, Transfer-Encoding: chunked
should work for requests (see RFC 2616).

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top