Author image Kazuhiro Osawa
and 1 contributors


HTTP::Request::StreamingUpload - streaming upload wrapper for HTTP::Request


upload from filepath

  my $req = HTTP::Request::StreamingUpload->new(
      PUT     => '',
      path    => '/your/upload.jpg',
      headers => HTTP::Headers->new(
          'Content-Type'   => 'image/jpeg',
          'Content-Length' => -s '/your/upload.jpg',
  my $res = LWP::UserAgent->new->request($req);

upload from filehandle

  open my $fh, '<', '/your/upload/requestbody' or die $!;
  my $req = HTTP::Request::StreamingUpload->new(
      PUT     => '',
      fh      => $fh,
      headers => HTTP::Headers->new(
          'Content-Length' => -s $fh,
  my $res = LWP::UserAgent->new->request($req);

upload from callback

  my @chunk = qw( foo bar baz );
  my $req = HTTP::Request::StreamingUpload->new(
      PUT      => '',
      callback => sub { shift @chunk },
      headers => HTTP::Headers->new(
          'Content-Type'   => 'text/plain',
          'Content-Length' => 9,
  my $res = LWP::UserAgent->new->request($req);


HTTP::Request::StreamingUpload is streaming upload wrapper for HTTP::Request. It could be alike when $DYNAMIC_FILE_UPLOAD of HTTP::Request::Common was used. However, it is works only for POST method with form-data. HTTP::Request::StreamingUpload works on the all HTTP methods.

Of course, you can big file upload using few memory by this wrapper.

HTTP::Request::StreamingUpload->new( $method, $uir, %args );

%args Options

headers => [ HeaderName => 'HeaderValue', ... ]
headers => { HeaderName => 'HeaderValue', ... }
headers => HTTP::Headers->new( HeaderName => 'HeaderValue', ... )

header is passed. HASHREF, ARRAYREF or HTTP::Headers object can be passed.

If you are possible, you should set up Content-Length for file upload. However, chunked upload for HTTP 1.1 will be performed by LWP::UserAgent if it does not set up.

path => '/your/file.txt'

set the upload file path.

fh => $fh

set the file-handle of upload file. It can use instead of path.

chunk_size => 4096

set the buffer size when reading the file of fh or path.

callback => sub { ...; return if $eof; return $buf }

Instead of path or fh, upload data is controlled by itself and can be made.

    # 10 times send epoch time
    callback => sub {
        return if $i++ > 10;
        return time() . "\n";


Kazuhiro Osawa <yappo <at> shibuya <döt> pl>


HTTP::Request, HTTP::Request::Common, HTTP::Headers, LWP::UserAgent


This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 167:

Non-ASCII character seen before =encoding in '<döt>'. Assuming UTF-8