Python: Testing your REST API
This documentation is part of the Creating a REST API guide. View the full guide here: How to create a REST API using Flask.
👋 Welcome to the Stackhero documentation!
Stackhero offers a ready-to-use Python cloud solution that provides a host of benefits, including:
- Deploy your application in seconds with a simple
git push.- Use your own domain name and benefit from the automatic configuration of HTTPS certificates for enhanced security.
- Enjoy peace of mind with automatic backups, one-click updates, and straightforward, transparent, and predictable pricing.
- Get optimal performance and robust security thanks to a private and dedicated VM.
Save time and simplify your life: it only takes 5 minutes to try Stackhero's Python cloud hosting solution!
Once the server is running, you can interact with it using cURL. Here are some examples:
-
Retrieve all tasks:
curl -s http://localhost:8080/api/tasks { "tasks": [ { "description": "Milk, Cheese, Pizza, Fruits", "done": false, "id": 1, "title": "Buy groceries" }, { "description": "Learn Python programming basics", "done": false, "id": 2, "title": "Learn Python" } ] } -
Retrieve task with ID 2:
curl -s http://localhost:8080/api/tasks/2 { "task": { "description": "Learn Python programming basics", "done": false, "id": 2, "title": "Learn Python" } } -
Create a new task:
curl -s -X POST -H "Content-Type: application/json" \ -d '{"title": "New task", "description": "Created with cURL"}' \ http://localhost:8080/api/tasks { "task": { "description": "Created with cURL", "done": false, "id": 3, "title": "New task" } }
Tip: Pipe the output to
jqto beautify the JSON. For example,curl -s http://localhost:8080/api/tasks/2 | jqproduces a more readable result.
Example of Python REST API using Flask, running in Stackhero Code-Hero, with the server (1) and the client using cURL (2)