Aan de slagGa gratis aan de slag

Receiving JSON with the requests package

When requesting JSON data from an API, the requests library makes it really easy to decode the JSON string you received from the API back into a Python object. In this exercise you'll first need to request data in the JSON format from the API, then decode the response into a Python object to retrieve and print the album Title property.

Note: The requests package has been imported for you, and because the albums API is protected by authentication, the correct header has already been added.

Deze oefening maakt deel uit van de cursus

Introduction to APIs in Python

Cursus bekijken

Oefeninstructies

  • Add the correct header to request JSON from the API.
  • Decode the JSON response into an album object.
  • Print the album Title property.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

headers = {
    'Authorization': 'Bearer ' + API_TOKEN,
    # Add a header to request JSON formatted data
    ____: ____
}
response = requests.get('http://localhost:3000/albums/1/', headers=headers)

# Get the JSON data as a Python object from the response object
album = ____

# Print the album title
print(____)
Code bewerken en uitvoeren