GET Request to Server
Lesson 9 of 10 in Coddy's API in Python course.
Challenge
MediumNow that you've established a proper connection with an API, let's send a GET request for a specific location.
- Send a GET request to:
https://wttr.in/London?format=j1(the?format=j1part tells the service to return JSON) - Save the
requests.get()object to a variable calledresponse - Parse the response using
response.json()and save it to a variable calleddata - The current conditions live inside
data["current_condition"][0]. Pull out the temperature (temp_C) and the description (weatherDesc[0]["value"]), then print both
Try it yourself
import requests
# Step 1: Send GET request for London weather (j1 = JSON format)
response = # Your code here
# Step 2: Parse JSON response
data = # Your code here
# Step 3: Print weather information
current = data["current_condition"][0]
print("Temperature:", current["temp_C"], "°C")
print("Description:", current["weatherDesc"][0]["value"])All lessons in API in Python
3Weather Fetch API Program
API Connection to ServerGET Request to ServerDisplaying Appropriate Results