On 28/05/2012 15:15, raphael() wrote:
> 
> I am trying to use HTTP::Tiny module to download file with RANGE attribute.
> However I cannot accomplish it.
> 
> In WWW::Mechanize the code I use is simple
> my $file_obj = $ua->get( $url, 'Range' =>  sprintf("bytes=%s-%s",
> $file_size, $content_length - 1), ) or die;
> 
> But I want to use the Tiny Module. How do I do this in HTTP::Tiny?
> 
> In the doc it says I have to give it a \%options hash-ref.
> my %options= { default_headers =>  "Range =>  bytes=0-999", };
> my $file_obj = $ua->get( $url, \%options ) or die;
> 
> something like above or am I sorely mistaken?
>
> The above code doesn't work. Even if half file exists the whole file is
> re-downloaded so range attribute doesn't work.
>
> I checked the length of downloaded content in debugger.  May be the hash
> ref thing I am doing is wrong!
> 
> A sample code will be most helpful.

Hi Raphael

HTTP::Tiny is very similar to WWW::Mechanize in this regard. The only
difference is that the request headers should be passed as a hash
reference instead of a plain hash, so you basically need a pair of
containing braces around what you have already written.

You shouldn't play with the default headers unless you want to limit the
download to the same range of bytes every time. Instead you should pass
specific values in the calls to the get method.

Also you cannot initialise a hash using

  my %options= { default_headers =>  "Range =>  bytes=0-999", };

as you are trying to assign an anonymous hash - a single scalar value -
to a hash that requires data in key/value pairs.

You can write

  my $response = $http->get($url, { headers => { Range => 'bytes=1-99' } } );

or you can set up the headers in a separate hash, like this

  my %headers = ( Range => 'bytes=1-99' );
  my $resp = $http->get('http://www.bbc.co.uk/', { headers => \%headers } );

Below is a complete working program for you to experiment with.

HTH,

Rob



  use strict;
  use warnings;

  use HTTP::Tiny;

  my $http = HTTP::Tiny->new;

  my %headers = ( Range => 'bytes=1-99' );
  my $resp = $http->get('http://www.bbc.co.uk/', { headers => \%headers } );

  print "@{$resp}{qw/ status reason /}\n\n";

  print $resp->{content}, "\n";



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to