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
Student
object (only the fields you want to update) in XML or JSON format. - Headers:
Content-Type
:application/json
orapplication/xml
(depending on your input format).Accept
:application/json
orapplication/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
(replace1
with the student’s ID). - Add headers:
Content-Type
:application/json
orapplication/xml
.Accept
:application/json
orapplication/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
id
in the URL path (/students/{id}
) must correspond to an existing student. - XML Structure:
- Use
<courses>
as the wrapper and<course>
for individual items (matching theStudent
model 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 givenid
does 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
id
in 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 |