Response code
- 1XX: Hold On: Something's happening. This is not final.
- 2XX: Here you go: Everything was successful. You should be getting the data you expected.
- 3XX: Go Away: It usually means you don't actually have permission to get this thing, so go away.
- 4XX: You screwed up: Either it could be a 404 which means that you screwed up and the thing you're looking for doesn't even exist,
- 5XX: I Screwed up: I, as in the server that you are making the request to, screwed up. And maybe the server is down, maybe the website is down or maybe there's some other issue.
import requests
response = requests.get(url="<http://api.open-notify.org/iss-now.json>")
print(response.status_code)
------------
200
Raise an exception for every single possible status code and telling the developer what might be the reason
import requests
response = requests.get(url="<http://api.open-notify.org/iss-now.json>")
if response.status.code == 404:
raise Exception("That resource does not exist.")
elif response.status_code == 401:
raise Exception("You are not authorised to access this data.")
Raise_for_status
import requests
response = requests.get(url="<http://api.open-notify.org/iss-now.json>")
response.raise_for_status()
data = response.json()
print(data)
---------------
{
"timestamp": 1632931270,
"iss_position": {
"latitude": "-51.2100",
"longitude": "-142.9537"
},
"message": "success"
}
import requests
response = requests.get(url="<http://api.open-notify.org/iss-now.json>")
response.raise_for_status()
data = response.json()
longitude = data["iss_posotion"]["longitude"]
latitude = data["iss_posotion"]["latitude"]
iss_position = (longitude, latitude)
print(iss_position)
---------------
("-51.2100", "-142.9537")