Skip to content

Instantly share code, notes, and snippets.

@tobyink
Created February 20, 2023 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobyink/aa1189902827f787f7d0124e6fe80253 to your computer and use it in GitHub Desktop.
Save tobyink/aa1189902827f787f7d0124e6fe80253 to your computer and use it in GitHub Desktop.
Perl HTTP+JSON example
use v5.16;
use HTTP::Tiny;
use JSON::PP 'decode_json';
my ( $lat, $lon ) = ( 54.5, -1.55 );
my $url = sprintf(
'https://api.open-meteo.com/v1/forecast?latitude=%s&longitude=%s'.
'&daily=temperature_2m_max,temperature_2m_min&timezone=Europe/London',
$lat,
$lon,
);
my $client = HTTP::Tiny->new;
my $response = $client->get( $url );
die "Failed!" unless $response->{success};
my $data = decode_json( $response->{content} );
printf(
"On %s: min temp %s, max temp %s\n",
$data->{daily}{time}[$_],
$data->{daily}{temperature_2m_min}[$_],
$data->{daily}{temperature_2m_max}[$_],
) for 0 .. 4;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment