Created
February 20, 2023 23:33
-
-
Save tobyink/aa1189902827f787f7d0124e6fe80253 to your computer and use it in GitHub Desktop.
Perl HTTP+JSON example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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