Sobes.tech
Senior

What HTTP methods were used in designing REST API? What is the difference between PATCH and POST?

sobes.tech AI

Answer from AI

When designing REST APIs, the following HTTP methods are typically used:

  • GET — retrieving a resource or list of resources.
  • POST — creating a new resource.
  • PUT — fully updating a resource.
  • PATCH — partially updating a resource.
  • DELETE — deleting a resource.

Difference between PATCH and POST:

  • POST is used for creating new resources or performing operations that are not idempotent. For example, submitting a form to create a record.

  • PATCH is used for partial updates of an existing resource. Only the fields that need to be changed are sent in the request, without replacing the entire object.

Example of a PATCH request to update a user's email:

PATCH /users/123 HTTP/1.1
Content-Type: application/json

{
  "email": "newemail@example.com"
}

POST, on the other hand, often looks like:

POST /users HTTP/1.1
Content-Type: application/json

{
  "name": "Ivan",
  "email": "ivan@example.com"
}

Thus, PATCH is more economical for updates, while POST is used for creation or executing actions.