1

I want to download a file in a Perl script.

If the download is OK, this works fine,

use File::Fetch;
$ff = File::Fetch->new(uri => 'http://some.where.com/dir/a.txt');
$where = $ff->fetch() or die $ff->error;

If there is an error, I want to see the http-response header. How can I do this?

1
  • 1
    http responses have several headers; can you show an example of what you mean? Commented Dec 29, 2014 at 23:00

2 Answers 2

1

Judging from the File::Fetch error handling documentation and source, all you get is a string containing the error details (e.g. via $ff->error in your example). Depending on what underlying module is used to perform the actual fetch, you might be able to ascertain the HTTP response info and parse it out. For example, if File::Fetch uses LWP, the following example error includes HTTP 500 in the error message:

Fetch failed! HTTP response: 500 Internal Server Error [500 Can't connect to some.where.com:80 (Bad hostname)] at fetch.pl line 8.

Please note that if you attempt to parse from the error string, you should be sure that you know which underlying module is being used to do the fetch (e.g. lwp, httptiny, wget, etc.) since the error message format is not identical.

If the HTTP response is really important to you, it may make more sense to switch to something like LWP where this info is consistently and reliably available programmatically without worrying about parsing (e.g. through a dedicated response object in the case of LWP).

Sign up to request clarification or add additional context in comments.

Comments

0

The following code should work if you use only HTTP URIs.

use HTTP::Tiny;
$http = HTTP::Tiny->new();
$response = $http->mirror( $uri, $file );
use Data::Dumper;
die Dumper($response) unless $response->{'success'};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.