To make a PATCH request to partially update a student using your API, follow the steps below. The PATCH endpoint in your controller is designed to update only the fields provided in the request body, leaving other fields unchanged.
- Endpoint:
PATCH /api/students/{id} - Purpose: Update specific fields of a student with the given
id. - Request Body: A partial
Studentobject (only the fields you want to update) in XML or JSON format. - Headers:
Content-Type:application/jsonorapplication/xml(depending on your input format).Accept:application/jsonorapplication/xml(for the response format).
JSON Example (update only the phone and course fields):
curl -X PATCH "'https://restapiapp.onrender.com/api/students/1" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-d '{
"phone": "9999999999",
"course": ["Computer Science", "Math"]
}'XML Example (update only the location and course fields):
curl -X PATCH "'https://restapiapp.onrender.com/api/students/1" \
-H "Content-Type: application/xml" \
-H "Accept: application/xml" \
-d '<student>
<location>Bangalore</location>
<courses>
<course>Computer Science</course>
</courses>
</student>'- Set the HTTP method to PATCH.
- Enter the URL:
'https://restapiapp.onrender.com/api/students/1(replace1with the student’s ID). - Add headers:
Content-Type:application/jsonorapplication/xml.Accept:application/jsonorapplication/xml.
- In the request body, provide only the fields to update in raw format (JSON or XML).
JSON Body (update name and phone):
{
"name": "Arvind Patched",
"phone": "8888888888"
}XML Body (update course list):
<student>
<courses>
<course>B.Sc</course>
</courses>
</student>fetch('https://restapiapp.onrender.com/api/students/1', {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
location: "Hyderabad",
course: ["B.Sc"]
})
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));- Partial Updates: Only include the fields you want to update in the request body. Omitted fields retain their existing values.
- ID Matching: The
idin the URL path (/students/{id}) must correspond to an existing student. - XML Structure:
- Use
<courses>as the wrapper and<course>for individual items (matching theStudentmodel annotations). - Example:
<student> <courses> <course>New Course</course> </courses> </student>
- Use
- Response:
200 OK: Success + message:"Student updated successfully".404 Not Found: If the student with the giveniddoes not exist.500 Internal Server Error: For server-side issues.
- Empty Body: The request body must contain at least one valid field to update.
- Data Types: Ensure
idin the URL is an integer (e.g.,1, not"1"). - XML Validation: Incorrect XML structure (e.g., missing
<courses>wrapper) will cause parsing errors.
| Feature | PUT | PATCH |
|---|---|---|
| Purpose | Replace all fields of a resource | Update specific fields |
| Request Body | Requires all fields | Requires only fields to update |
| Idempotent | Yes (repeated requests have the same effect) | Not necessarily idempotent |