To make a PUT request to update a student using your API, follow the steps below. The PUT endpoint in your controller is designed to replace all fields of an existing student with the data provided in the request body.
- Endpoint:
PUT /api/students/{id} - Purpose: Update all fields of a student with the given
id. - Request Body: A
Studentobject in XML or JSON format. - Headers:
Content-Type:application/jsonorapplication/xml(depending on your input format).Accept:application/jsonorapplication/xml(depending on your desired response format).
JSON Example:
curl -X PUT "https://restapiapp.onrender.com/api/students/1" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-d '{
"id": 1,
"name": "Arvind Updated",
"location": "Mumbai",
"phone": "9876543210",
"course": ["MCA"]
}'XML Example:
curl -X PUT "https://restapiapp.onrender.com/api/students/1" \
-H "Content-Type: application/xml" \
-H "Accept: application/xml" \
-d '<student>
<id>1</id>
<name>Arvind Updated</name>
<location>Mumbai</location>
<phone>9876543210</phone>
<courses>
<course>MBA</course>
<course>MCA</course>
</courses>
</student>'- Set the HTTP method to PUT.
- 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 the updated student data in raw format (JSON or XML).
JSON Body:
{
"id": 1,
"name": "Arvind Updated",
"location": "Mumbai",
"phone": "9876543210",
"course": ["MBA", "MCA"]
}XML Body:
<student>
<id>1</id>
<name>Arvind Updated</name>
<location>Mumbai</location>
<phone>9876543210</phone>
<courses>
<course>MBA</course>
</courses>
</student>fetch('https://restapiapp.onrender.com/api/students/1', {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
id: 1,
name: "Arvind Updated",
location: "Mumbai",
phone: "9876543210",
course: ["MBA", "MCA"]
})
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));- ID Matching: The
idin the URL path (/students/{id}) must match theidin the request body. - Field Requirements: All fields (
name,location,phone,course) must be provided in the request body (PUT replaces the entire resource). - Response:
200 OK: Success + message:"Student updated successfully".404 Not Found: If the student with the giveniddoes not exist.500 Internal Server Error: If there’s a server-side issue.
- XML Structure: Ensure the XML request body uses
<courses>as the wrapper and<course>for individual items. - Data Types: The
idis an integer (int), not a string. - Validation: If the
courselist is empty or fields likenameare missing, the API may throw errors (depending on your validation rules).