DEV Community

Hemant Govekar
Hemant Govekar

Posted on Edited on

Explain PUT POST and PATCH

In HTTPS, the HTTP methods PUT, POST, and PATCH are used to perform operations on resources. Here's a differentiation of their purposes and behavior:

  1. POST:
  • Purpose: Used to create new resources or submit data to a server. It appends the resource to an existing collection.
  • Idempotency: Not idempotent; multiple identical POST requests will create multiple resources.
  • Example: Submitting a registration form to create a new user.
  • Response: Usually returns 201 Created and a URI pointing to the newly created resource.
  1. PUT:
  • Purpose: Used to create or completely replace a resource at a specific URI. If the resource exists, it is updated; if it doesn't, it is created.
  • Idempotency: Idempotent; multiple identical PUT requests will produce the same result (i.e., the same resource state).
  • Example: Updating an entire profile with a new set of data.
  • Response: Typically returns 200 OK (if updated) or 201 Created (if new resource is created).
  1. PATCH:
    • Purpose: Used to partially update an existing resource. Only the fields provided in the request will be updated, leaving the rest unchanged.
    • Idempotency: Idempotent; multiple identical PATCH requests will result in the same resource state.
    • Example: Updating just the email address of a user profile without altering other fields.
    • Response: Commonly returns 200 OK or 204 No Content.

Key Differences

Method Action Idempotency Typical Use Case
POST Create new resource No Submitting forms or adding records to a database
PUT Replace entire resource Yes Overwriting or creating a resource at a specific URI
PATCH Update part of a resource Yes Modifying specific fields in a resource

Each method is designed for specific operations, ensuring clarity and proper organization of HTTP operations.

Top comments (2)

Collapse
 
aein_bagheri_10ffa69696b9 profile image
Aein

سلام

Collapse
 
hemantgovekar profile image
Hemant Govekar

hello brother