REST API and Common HTTP Methods
Source: Dev.to
Real APIs Used in This Article
-
Dog API (no key required) – Real dog breeds and images
https://dog.ceo/api -
ReqRes API (mock API) – For POST, PUT, PATCH, DELETE examples
https://reqres.in/api
Both APIs return JSON and require no authentication.
Key Principles of REST
- Stateless – Each request is independent.
- Client–Server separation – Frontend and backend are independent.
- Uniform interface – Uses standard HTTP methods.
- Resource‑based – Everything is treated as a resource.
Common HTTP Methods in REST APIs
1. GET – Read Data (Dog API)
Used to fetch data from the server.
Example: Get a random dog image
GET https://dog.ceo/api/breeds/image/random
- Retrieves a random dog image.
- Does not modify server data.
Example: List all dog breeds
GET https://dog.ceo/api/breeds/list/all
- Returns a JSON object with all dog breeds.
2. POST – Create a Resource (ReqRes API)
Used to create something new.
Example: Create a user
POST https://reqres.in/api/users
Content-Type: application/json
{
"name": "Dog Lover",
"role": "Trainer"
}
- Creates a new user.
- Returns a user object with an ID.
3. PUT – Update Entire Resource (ReqRes API)
Used to replace an existing resource completely.
Example: Replace a user
PUT https://reqres.in/api/users/2
Content-Type: application/json
{
"name": "Dog Expert",
"role": "Veterinarian"
}
- Replaces all fields of the resource.
4. PATCH – Update Partial Resource (ReqRes API)
Used to update specific fields of a resource.
Example: Update a user’s role
PATCH https://reqres.in/api/users/2
Content-Type: application/json
{
"role": "Dog Therapist"
}
- Updates only the
rolefield. - More efficient than PUT for partial changes.
5. DELETE – Remove Resource (ReqRes API)
Used to delete a resource.
Example: Delete a user
DELETE https://reqres.in/api/users/2
- Deletes the user with ID
2. - Returns a 204 No Content status.
Common HTTP Status Codes
- 200 OK – Request successful.
- 201 Created – New resource created.
- 204 No Content – Delete successful.
- 400 Bad Request – Invalid input.
- 401 Unauthorized – Authentication needed.
- 404 Not Found – Resource not found.
- 500 Internal Server Error – Server problem.
Why REST APIs Are So Popular
- Easy to understand and implement.
- Language and platform independent.
- Uses lightweight JSON format.
- Scales well for large systems.
- Supported by all modern frameworks.
Final Thoughts
REST APIs power real‑world applications — even fun ones like dog image apps 🐶! Using real endpoints like the Dog API lets you experiment with GET requests directly, while mock APIs like ReqRes help you practice full CRUD methods. Try these endpoints in your browser, Postman, or code — and you’ll learn REST the right way 🚀
Happy coding!