Skip to content
thesarfo

Note

Consuming a REST API with requests

Fetching JSON from a REST API with the requests library, raising for bad status codes, and pulling specific keys out of the response.

views 0

we import the module for handling apis

import requests

parse the url to get the response from

response = requests.get(url='http://api.open-notify.org/iss-now.json')

raising an error in case we dont get the desired response code

response.raise_for_status()

parse the response in json format

data = response.json()

get specific data from the json using keys

longitude = data['iss_position']['longitude']
latitude = data['iss_position']['latitude']
iss_position = (longitude, latitude)
print(iss_position)