PUT vs PATCH

๐Ÿ’ก Concept Name

PUT vs PATCH โ€“ Both are HTTP methods used for updating resources in RESTful APIs. PUT replaces the entire resource, while PATCH applies partial modifications.

๐Ÿ“˜ Quick Intro

PUT requires sending a complete representation of the resource to overwrite it fully. In contrast, PATCH sends only the changes to update specific fields. Both are idempotent but serve different purposes.

๐Ÿง  Analogy / Short Story

Think of updating your rรฉsumรฉ: using PUT is like rewriting and submitting the entire document, even if you only updated one detail. PATCH is like correcting just that one line and saving the change.

๐Ÿ”ง Technical Explanation

  • ๐Ÿ“ฆ PUT: Fully replaces the target resource with the data provided.
  • ๐Ÿฉน PATCH: Applies partial updates to one or more fields.
  • ๐Ÿงพ Payload: PUT requires the entire resource representation; PATCH only the modified attributes.
  • โ™ป๏ธ Idempotency: Both methods are idempotent, meaning repeating them results in the same final state.
  • ๐Ÿ› ๏ธ When to Use: Use PUT for full replacements, PATCH for partial modifications.

๐ŸŽฏ Purpose & Use Case

  • โœ… Use PUT when replacing an entire user profile or configuration file.
  • โœ… Use PATCH for quick edits, like updating an email or status field.

๐Ÿ’ป Real Code Example


// PUT - full replacement
app.put('/user/:id', (req, res) => {
  const user = { ...req.body, id: parseInt(req.params.id) };
  res.json({ message: 'User replaced via PUT', user });
});

// PATCH - partial update
app.patch('/user/:id', (req, res) => {
  user = { ...user, ...req.body };
  res.json({ message: 'User updated via PATCH', user });
});
            

โ“ Interview Q&A

Q1: What is the key difference between PUT and PATCH?
A: PUT replaces the entire resource; PATCH updates only specified parts.

Q2: Is PUT idempotent?
A: Yes, sending the same PUT request multiple times yields the same result.

Q3: When is PATCH preferable over PUT?
A: When updating a few fields without affecting the whole resource.

Q4: Does PUT require full resource data?
A: Yes, it expects a complete resource representation.

Q5: Can PATCH be used for large updates?
A: Yes, especially to avoid sending the entire resource.

๐Ÿ“ MCQs

Q1. Which HTTP method fully replaces a resource?

  • PATCH
  • PUT
  • GET
  • POST

Q2. Which HTTP method allows partial updates?

  • GET
  • PUT
  • POST
  • PATCH

Q3. Is PUT idempotent?

  • Yes
  • No
  • Only for GET
  • Depends on implementation

Q4. What does PATCH do?

  • Deletes fields
  • Replaces entire object
  • Creates new object
  • Updates specific fields

Q5. Which method is better for updating a single field?

  • GET
  • PUT
  • PATCH
  • DELETE

Q6. PUT requires:

  • Partial data
  • No data
  • Complete resource representation
  • Only an ID

Q7. PATCH is useful when:

  • Deleting resources
  • Reading metadata
  • Creating new IDs
  • Updating parts of a resource

Q8. PATCH is typically:

  • Unsafe
  • Non-idempotent
  • Idempotent
  • Unreliable

Q9. Which HTTP method sends heavier payloads?

  • PATCH
  • PUT
  • HEAD
  • OPTIONS

Q10. Which method overwrites an object?

  • PATCH
  • GET
  • PUT
  • POST

๐Ÿ’ก Bonus Insight

Some APIs implement PATCH using the JSON Patch standard (RFC 6902), which defines a set of operations like add, remove, and replace. This allows precise, operation-based updates but can be more complex to handle.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: