Skip to content

Latest commit

 

History

History
126 lines (105 loc) · 4 KB

PATCH.md

File metadata and controls

126 lines (105 loc) · 4 KB

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.


PATCH Request Details

  • 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 or application/xml (depending on your input format).
    • Accept: application/json or application/xml (for the response format).

Example Requests

1. Using curl (Command Line)

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>'

2. Using Postman

  1. Set the HTTP method to PATCH.
  2. Enter the URL: 'https://restapiapp.onrender.com/api/students/1 (replace 1 with the student’s ID).
  3. Add headers:
    • Content-Type: application/json or application/xml.
    • Accept: application/json or application/xml.
  4. 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>

3. Using JavaScript (fetch API)

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));

Key Notes

  1. Partial Updates: Only include the fields you want to update in the request body. Omitted fields retain their existing values.
  2. ID Matching: The id in the URL path (/students/{id}) must correspond to an existing student.
  3. XML Structure:
    • Use <courses> as the wrapper and <course> for individual items (matching the Student model annotations).
    • Example:
      <student>
          <courses>
              <course>New Course</course>
          </courses>
      </student>
  4. Response:
    • 200 OK: Success + message: "Student updated successfully".
    • 404 Not Found: If the student with the given id does not exist.
    • 500 Internal Server Error: For server-side issues.

Troubleshooting

  • 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.

PUT vs. PATCH

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