API Connection to Server
Lesson 8 of 10 in Coddy's API in Python course.
Challenge
MediumOpenWeatherMap typically requires an API key, but for learning purposes, we'll use a free alternative that doesn't require sign-up: Open-Meteo API.
Send a GET request to: https://api.open-meteo.com/v1/forecast
Add the following parameters using params:
{
"latitude": 52.52,
"longitude": 13.41,
"current_weather": "true"
}Save the requests.get() object to a variable called response
Parse the response using response.json() and save it to a variable called data
Print the current temperature by accessing: data["current_weather"]["temperature"]
Try it yourself
import requests
# Step 1: Send GET request with parameters
response = requests.get(
# Your URL here
params={
# Your parameters here
}
)
# Step 2: Parse JSON response
data = # Your code here
# Step 3: Print current temperature
print("Current temperature:", data["current_weather"]["temperature"], "°C")All lessons in API in Python
3Weather Fetch API Program
API Connection to ServerGET Request to ServerDisplaying Appropriate Results