How REST APIs use HTTP methods
· Category: API & REST
Short answer
REST APIs use standard HTTP methods to perform operations on resources, creating a uniform interface that clients and servers can rely on.
Steps
- Use GET to retrieve a representation of a resource without side effects.
- Use POST to create a new subordinate resource or trigger a process.
- Use PUT to replace an existing resource entirely with a new representation.
- Use PATCH to apply partial modifications to a resource.
- Use DELETE to remove a resource identified by its URI.
Tips
- Design endpoints around nouns representing resources rather than verbs.
- Use plural names for collections such as /orders rather than /order.
- Return the created resource or a location header after POST requests.
- Ensure idempotency for PUT and DELETE so retries are safe.
Common issues
- Using POST for all operations and ignoring the semantic benefits of other methods.
- Non-idempotent PUT implementations causing duplicate resources on retry.
- Returning error codes that do not match the actual failure mode.
- Confusing PATCH with PUT and accidentally overwriting fields.
Example
curl -X GET https://api.example.com/users -H "Accept: application/json" -H "Authorization: Bearer $TOKEN"
This curl command demonstrates a standard GET request with headers for content negotiation and bearer token authentication.