1. Learn
  2. /
  3. Courses
  4. /
  5. Introduction to APIs in Python

Connected

Exercise

Sending JSON with the requests package

Similar to how you can receive JSON text from an API response, you can also send JSON text to an API with POST or PUT requests. If you use the json argument for the request.post() and request.put() methods, the requests library will take care of adding all the necessary headers and encoding for you. Neat!

Let's try it out! Did you know you can create multiple playlists at once using a POST request to the /playlists API? Just pass an array of playlists (each with a Name property) to the API and it will create them all at once.

Instructions

100 XP
  • Pass the playlists variable as an argument to the requests.post() method so that it will be automatically sent as JSON.
  • Get a list of all playlists from the API.
  • Inspect the response of the GET request by printing the JSON text.