-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12_PATCH_vs_PUT.js
More file actions
25 lines (25 loc) · 1.05 KB
/
12_PATCH_vs_PUT.js
File metadata and controls
25 lines (25 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* 12. Difference between PATCH and PUT
*
* Both are HTTP methods used to update resources, but they have different semantics.
*
* PUT (Replace):
* - It is idempotent (calling it multiple times has the same effect).
* - It REPLACES the entire resource at the target URL with the request payload.
* - If you send a partial object, the fields missing in the payload might be set to null or default in the database (depending on backend implementation), effectively deleting them.
*
* Example:
* Original User: { id: 1, name: "John", age: 30 }
* PUT /users/1 with { name: "Jane" }
* Result User: { id: 1, name: "Jane", age: null } (Age is lost if not handled specifically)
*
* PATCH (Modify):
* - It is NOT necessarily idempotent (though often implemented as such).
* - It applies PARTIAL updates to the resource.
* - Used to modify only specific fields without affecting others.
*
* Example:
* Original User: { id: 1, name: "John", age: 30 }
* PATCH /users/1 with { name: "Jane" }
* Result User: { id: 1, name: "Jane", age: 30 } (Age is preserved)
*/