PUT vs PATCH Requests in API Testing

PUT vs PATCH Requests in API Testing


Both PUT and PATCH are HTTP methods used to update resources on a server. However, they differ in how the updates are applied.


1. PUT (Update Entire Resource)

  • Purpose: Replaces the entire resource with the new data provided in the request body.
  • Idempotent: Yes (making the same request multiple times results in the same outcome).
  • Use Case: Use when you need to update all fields of a resource or completely replace it.

Example

Scenario: Update a user's profile information.

  • Request URL: PUT /users/123
  • Request Body:

{
  "name": "John Doe",
  "email": "john.doe@example.com",
  "age": 30
}
        

  • Response:

{
  "message": "User updated successfully",
  "user": {
    "id": 123,
    "name": "John Doe",
    "email": "john.doe@example.com",
    "age": 30
  }
}
        

Key Points:

  • If any field is missing in the request, it is removed or set to default on the server. For example, omitting age would remove it from the resource.


2. PATCH (Update Partial Resource)

  • Purpose: Updates only the specified fields in the resource.
  • Idempotent: Yes.
  • Use Case: Use when you need to update specific fields without affecting other parts of the resource.

Example

Scenario: Update only the email of a user.

  • Request URL: PATCH /users/123
  • Request Body:

{
  "email": "john.new@example.com"
}        

  • Response:

{
  "message": "User updated successfully",
  "user": {
    "id": 123,
    "name": "John Doe",
    "email": "john.new@example.com",
    "age": 30
  }
}
        

Key Points:

  • Fields not included in the request body remain unchanged. For example, name and age remain the same.


Comparison Table

Article content
PUT vs PATCH Requests in API Testing

Conclusion

  • Use PUT when updating all fields or replacing a resource entirely.
  • Use PATCH for partial updates to specific fields, ensuring minimal impact on the resource.


𝐇𝐚𝐩𝐩𝐲 𝐓𝐞𝐬𝐭𝐢𝐧𝐠!

#AutomationTesting#Selenium#SoftwareTesting#QA#Testers

To view or add a comment, sign in

Others also viewed

Explore topics