Python: 測試您的 REST API
此文件屬於建立 REST API指南的一部分。請在此處查看完整指南:如何使用 Flask 建立 REST API。
👋 歡迎來到 Stackhero 文檔!
Stackhero 提供一個即用型的 Python 雲端 解決方案,帶來多項好處,包括:
- 只需一個簡單的
git push,即可在幾秒鐘內部署您的應用程式。- 使用您自己的域名,並享受 HTTPS 證書的自動配置以增強安全性。
- 享受自動備份、一鍵更新,以及簡單、透明且可預測的定價,讓您安心無憂。
- 得益於專用的私人 VM,獲得最佳的性能和強大的安全性。
節省時間並簡化您的生活:只需 5 分鐘即可嘗試 Stackhero 的 Python 雲端託管 解決方案!
伺服器運行後,您可以使用 cURL 與其互動。以下是一些範例:
-
檢索所有任務:
curl -s http://localhost:8080/api/tasks { "tasks": [ { "description": "牛奶、奶酪、披薩、水果", "done": false, "id": 1, "title": "買雜貨" }, { "description": "學習 Python 程式設計基礎", "done": false, "id": 2, "title": "學習 Python" } ] } -
檢索 ID 為 2 的任務:
curl -s http://localhost:8080/api/tasks/2 { "task": { "description": "學習 Python 程式設計基礎", "done": false, "id": 2, "title": "學習 Python" } } -
創建新任務:
curl -s -X POST -H "Content-Type: application/json" \ -d '{"title": "新任務", "description": "使用 cURL 創建"}' \ http://localhost:8080/api/tasks { "task": { "description": "使用 cURL 創建", "done": false, "id": 3, "title": "新任務" } }
提示:將輸出導入
jq以美化 JSON。例如,curl -s http://localhost:8080/api/tasks/2 | jq會產生更易讀的結果。
使用 Flask 的 Python REST API 範例,運行於 Stackhero Code-Hero,伺服器 (1) 和使用 cURL 的客戶端 (2)