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

  1. Use GET to retrieve a representation of a resource without side effects.
  2. Use POST to create a new subordinate resource or trigger a process.
  3. Use PUT to replace an existing resource entirely with a new representation.
  4. Use PATCH to apply partial modifications to a resource.
  5. 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.