Searched for tag HTTP and found 2 results in 0.6 ms

Perl: Fetch HTTPS content

If you need to fetch a remote URL via HTTPS in a Perl script the easiest way I have found is to use HTTP::Tiny. HTTP::Tiny is a core module, and included in all Perl installations.

Sample code:

use HTTP::Tiny;

my $http = HTTP::Tiny->new(verify_SSL => 1, timeout => 5);
my $resp = $http->get("https://www.perturb.org/");
my $body = $resp->{content};

print $body;
Tags:
Leave A Reply

Perl: Fetch data from a URL

I need a quick way to fetch JSON data from a URL. Using HTTP::Tiny is the easiest way. You can call the function different ways depending on whether you only want the body, or if you want the HTTP status code as well:

my $json = get_url("https://domain.com/stats.json");

my ($html, $http_code) = get_url("https://domain.com/index.html");
sub get_url {
    my $url = shift();

    my $http = HTTP::Tiny->new(verify_SSL => 1, timeout => 5);
    my $resp = $http->get($url);

    my $body   = $resp->{content};
    my $status = $resp->{status};

    my @ret = ($body, $status, $resp);

    return @ret;
}
Tags:
Leave A Reply