A PATCH request is used to partially update an existing resource on a server. In Postman, we can use PATCH requests to test APIs that update only selected fields of an existing record.
A PATCH request is an HTTP request used to make a partial update to an existing resource.
For example:
PATCH /students/10
This request can update selected information of student ID 10.
Postman provides an easy way to create and test PATCH requests.
The basic process is:
Open Postman and create a new HTTP request.
You will see the HTTP method dropdown, URL field, and Send button.
These controls are used to create and send the PATCH request.
Click the HTTP method dropdown and select PATCH.
PATCH
PATCH tells the server that the client wants to make a partial update.
Enter the URL of the API that will update the existing resource.
Example:
http://localhost/api/students.php?id=10
The API can use the student ID to identify the record that should be updated.
A PATCH request commonly contains only the fields that need to be changed.
For example, if we only want to change the course:
{
"course": "Python"
}
There is no need to send every student field when the API supports partial updates.
After selecting PATCH, click the Body tab in Postman.
The Body section is used to enter the fields that should be updated.
Select raw in the Body section.
The raw option allows you to directly enter the request data.
If the API accepts JSON, select JSON from the format dropdown.
Example:
{
"mobile": "9876543210"
}
Enter only the information that you want to change.
{
"course": "Java"
}
The API can update only the course field of the selected student.
The API needs to know which record should be updated.
The resource ID can be included in the URL.
PATCH http://localhost/api/students.php?id=10
The ID 10 identifies the student to update.
When JSON data is sent, the request should indicate that the request body contains JSON.
Content-Type: application/json
Postman can add this header automatically when JSON is selected.
After entering the partial update data, click the Send button.
Postman sends the PATCH request to the API server.
PATCH
↓
API URL
↓
Request Body
↓
Server
The server receives the PATCH request and reads the request body.
A PHP REST API can decode the JSON data and update only the provided fields.
PATCH Request
↓
PHP API
↓
Read JSON
↓
Validate Data
↓
Partial Database Update
A PHP REST API can use PATCH data to update selected columns in a MySQL record.
For example:
PATCH JSON
↓
PHP API
↓
MySQL UPDATE
↓
Selected Field Updated
Suppose student ID 10 has the following information:
{
"name": "Rahul Kumar",
"course": "ADCA",
"mobile": "9876543210"
}
If we only want to change the course, we can send:
{
"course": "Python"
}
The other student information can remain unchanged.
PATCH can also update more than one selected field.
Example:
{
"course": "Java",
"mobile": "9123456789"
}
Only the specified fields are sent for the partial update.
After successfully updating the resource, the API can return a JSON response.
{
"status": true,
"message": "Student updated successfully"
}
The response informs the client that the update was completed.
A successful PATCH request commonly returns:
200 OK
This indicates that the server successfully processed the update.
If the requested student does not exist, the API should return an appropriate error response.
For example:
{
"status": false,
"message": "Student not found"
}
The API should check the resource before performing the update.
The API should validate the fields received in the PATCH request.
For example, if a mobile number is being changed, the API can check whether the value has a valid format.
{
"mobile": "9876543210"
}
Validation helps prevent invalid data from being stored.
| PUT | PATCH |
|---|---|
| Commonly used to update or replace a resource | Commonly used for a partial update |
| May send the complete resource | Usually sends only changed fields |
| Example: Update complete student | Example: Update only student course |
| POST | PATCH |
|---|---|
| Commonly used to create a resource | Commonly used to partially update a resource |
| Example: Add student | Example: Change student course |
React Native applications can use PATCH requests when only part of a record needs to be changed.
Examples include:
React Native App
↓
PATCH Request
↓
PHP REST API
↓
Validate Data
↓
MySQL UPDATE
↓
JSON Response
↓
React Native App
Postman can be used to test the PATCH API before connecting it to the mobile application.
If a PATCH request does not work, check the following:
1. Open Postman
2. Create a request
3. Select PATCH
4. Enter API URL
5. Add resource ID
6. Open Body
7. Select raw
8. Select JSON
9. Enter fields to update
10. Click Send
11. Check the response
Method:
PATCH
URL:
http://localhost/api/students.php?id=10
Body:
{
"course": "Java",
"mobile": "9123456789"
}
Click Send to update only the selected student fields.
A PATCH request is commonly used for partial updates. In Postman, select PATCH, enter the API URL with the resource identifier, provide only the fields that need to be changed in the JSON body, and click Send.
PATCH
↓
Resource ID
↓
Selected JSON Fields
↓
PHP REST API
↓
Validation
↓
MySQL UPDATE
↓
JSON Response
Question: Which HTTP method is commonly used to partially update an existing resource?