HTTP Methods: Choosing the Right Action
GET — Fetch data without changing anything. Use this when you want to retrieve information. The URL includes parameters to specify what you want.
Example: GET https://api.example.com/users?id=42 — fetch user 42's information.
POST — Create something new. Send data in the request body. The server creates a new record and returns it.
Example: POST https://api.example.com/tasks with body {"title": "Call the dentist", "due_date": "2024-03-15"} — create a new task.
PUT — Replace something entirely. Send the complete updated data.
Example: PUT https://api.example.com/users/42 with the complete updated user object — replace user 42 with this new data.
DELETE — Remove something.
Example: DELETE https://api.example.com/tasks/100 — delete task 100.
PATCH — Partially update something. Send only the fields you want to change.
Example: PATCH https://api.example.com/users/42 with body {"status": "inactive"} — change just the status field of user 42.
The API documentation tells you which method to use. Always follow the documentation — using the wrong method can cause unexpected behavior.