can LWP handle this?

Discussion in 'Perl Misc' started by Larry, Oct 21, 2008.

  1. Larry

    Larry Guest

    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
     
    Larry, Oct 21, 2008
    #1
    1. Advertising

  2. Larry

    Larry Guest

    In article <>,
    Larry <> wrote:

    > 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;
     
    Larry, Oct 21, 2008
    #2
    1. Advertising

  3. On 2008-10-21 15:39, Larry <> wrote:
    > In article <>,
    > Larry <> wrote:
    >> 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
     
    Peter J. Holzer, Oct 21, 2008
    #3
  4. Larry

    Larry Guest

    In article <>,
    "Peter J. Holzer" <> wrote:

    > 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!
     
    Larry, Oct 22, 2008
    #4
  5. Larry

    Larry Guest

    In article <>,
    Larry <> wrote:

    > 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
     
    Larry, Oct 22, 2008
    #5
  6. Larry

    C.DeRykus Guest

    On Oct 22, 9:20 am, Larry <> wrote:
    > In article <>,
    >
    > Larry <> wrote:
    > > 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?
    >


    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);
    ....

    --
    Charles DeRykus
     
    C.DeRykus, Oct 23, 2008
    #6
  7. Larry

    Larry Guest

    In article <>,
    Larry <> wrote:

    > if $_hsent == 0
    > {
    > return "Hello World!";
    > $_hsent = 1;


    i should have swappeed them:

    $_hsent = 1;
    return "Hello World!";
     
    Larry, Oct 24, 2008
    #7
  8. On 2008-10-22 10:32, Larry <> wrote:
    > In article <>,
    > "Peter J. Holzer" <> wrote:
    >> 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!


    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
     
    Peter J. Holzer, Oct 25, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dietrich
    Replies:
    1
    Views:
    656
    Joe Smith
    Jul 22, 2004
  2. Leon
    Replies:
    2
    Views:
    546
  3. =?ISO-8859-1?Q?KLEIN_St=E9phane?=
    Replies:
    3
    Views:
    461
    hanumizzle
    Oct 6, 2006
  4. Karra
    Replies:
    2
    Views:
    783
    Octavian Rasnita
    Dec 29, 2010
  5. CronJob
    Replies:
    5
    Views:
    167
    Eric Pozharski
    Mar 20, 2009
Loading...

Share This Page