使用 API 建立與刪除資源
現在你已經學會如何組合 URL,就能對特定的 API 資源送出請求了。來看看在這些資源上,HTTP 動詞還能做些什麼。
在本練習中,你會使用可透過 http://localhost:3000/playlists/ 存取的「播放清單 API」。此 API 提供以下動作:
| 動詞 | 路徑 | 說明 |
|---|---|---|
| GET | playlists |
取得所有播放清單的清單 |
| GET | /playlists/{PlaylistId} |
使用唯一識別碼 PlaylistId 取得單一播放清單的資訊 |
| POST | /playlists |
建立新的播放清單 |
| DELETE | /playlists/{PlaylistId} |
使用唯一識別碼 PlaylistId 移除既有的播放清單 |
你會先取得所有現有播放清單的清單,接著學習如何建立新的播放清單並驗證是否已建立,最後學習如何移除既有的播放清單。
為方便起見,已經替你匯入 requests 函式庫。
本練習屬於課程
Python API 入門
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Get a list of all playlists from the API
response = requests.____('____')
print(response.text)