content

PATCH

In REST, PATCH is an HTTP method used to partially update an existing resource — meaning, you only change some fields instead of replacing the entire record.

Method Purpose Description
GET Read Retrieve data
POST Create Add a new resource
PUT Replace Update all fields (full update)
PATCH Modify Update specific fields (partial update)
DELETE Remove Delete a resource

Code

// **NEW** Partially update a Student
app.patch("/students/:id", (req, res) => {
  const id = parseInt(req.params.id);
  const { name, course } = req.body;

  const student = students.find(s => s.id === id);
  if (!student) {
    return res.status(404).json({ error: "Student not found" });
  }

  if (name !== undefined) student.name = name;
  if (course !== undefined) student.course = course;

  res.json(student);
});

Test

curl -X PATCH http://localhost:3000/students/4 \
  -H "Content-Type: application/json" \
  -d '{"course":"Statistics"}'

GIT

Do not forget

Azure

Deploy