http://www.perlmonks.org?node_id=761562


in reply to Re: POST'ing a large File with LWP::UserAgent
in thread POST'ing a large File with LWP::UserAgent

#!/usr/bin/perl -- use strict; use warnings; use LWP 5.825; my $url = "http://foo/upload"; my $file = __FILE__;#"foo/bar"; my $ua = LWP::UserAgent->new; $HTTP::Request::Common::DYNAMIC_FILE_UPLOAD = 1; use HTTP::Request::Common; my $req = POST( $url, Content_Type => 'multipart/form-data', Content => [ file => [$file] ], ); # set up callback { my $gen = $req->content(); die unless ref($gen) eq "CODE"; my $i = 0; $req->content( sub { my $chunk = &$gen(); # get chunk of data warn $i++; return $chunk; # send it to $url } ); #use Data::Dumper;die print Data::Dumper->new([($ua,$req, $gen)])->Ind +ent(1)->Deparse(1)->Dump; } my $res = $ua->request($req); #do it print $res->status_line;

Replies are listed 'Best First'.
Re^3: POST'ing a large File with LWP::UserAgent
by Anonymous Monk on Nov 30, 2011 at 06:34 UTC

    Thanks for the feed back. Hi can you explain what the below code actually do

    # set up callback { my $gen = $req->content(); die unless ref($gen) eq "CODE"; my $i = 0; $req->content( sub { my $chunk = &$gen(); # get chunk of data warn $i++; return $chunk; # send it to $url } );

      Thanks for the feed back. Hi can you explain what the below code actually do

      Hi. There is no way you were overrider ;)

      The code saves the default (DYNAMIC_FILE_UPLOAD) callback for uploading a large file, and wraps it in another callback, which invokes the original callback, but also counts the number of times it was called, and reports the number stderr