In [1]:
import csv
import requests
200 -- everything went okay, and the result has been returned (if any) 301 -- the server is redirecting you to a different endpoint. This can happen when a company switches domain names, or an endpoint name is changed. 401 -- the server thinks you're not authenticated. This happens when you don't send the right credentials to access an API (we'll talk about this in a later mission). 400 -- the server thinks you made a bad request. This can happen when you don't send along the right data, among other things. 403 -- the resource you're trying to access is forbidden -- you don't have the right permissions to see it. 404 -- the resource you tried to access wasn't found on the server.
In [ ]:
response = requests.get("http://api.open-notify.org/iss-now.json")
response.status_code
In [3]:
# Set up the parameters we want to pass to the API.
# This is the latitude and longitude of New York City.
parameters = {"lat": 40.71, "lon": -74}
# Make a get request with the parameters.
response = requests.get("http://api.open-notify.org/iss-pass.json", params=parameters)
# Print the content of the response (the data the server returned)
print(response.content)
# This gets the same data as the command above
response = requests.get("http://api.open-notify.org/iss-pass.json?lat=40.71&lon=-74")
print(response.content)
Forcing everithing in to JSON JavaScript Object Notation is teh way to encode data structures, like lists and dictionaries JSON is the primary format in which informaton is passed in applications. Python has great support with the JSON librarie and converts lists to string and to dictionaries. The json library has two main methods:
dumps -- Takes in a Python object, and converts it to a string. loads -- Takes a json string, and converts it to a Python object.