Skip to content
CodeKruPro

Partial Update with PATCH

21/62

Partial Update with PATCH

PATCH updates only the specified fields, keeping the rest unchanged. This is the key difference from PUT.

User 1 is John Doe from New York. Let's change only the city.

Task:

  1. PATCH /users/1 with body {"city": "San Francisco"}
  2. Notice the name stays "John Doe" while only city changes

Expected output:

Status: 200
Name: John Doe
City: San Francisco

About this lesson

REST Assured PATCH request: a partial update that leaves other fields alone

Learn REST AssuredIntermediatePUT and PATCH

How it works

PATCH sends a set of changes instead of a full replacement. You name only the fields you want different, and the server merges them into the stored record. That makes it lighter than PUT and safer when several clients edit different fields of the same resource.

On testkru, PATCH /users/1 takes whatever JSON object you send and merges it over user 1, then returns the whole merged record with all five fields. Send only a new city and the response still carries John Doe, his email and age 30, with the city swapped. An empty object {} counts as no body at all and is refused with 400, as is a request with no JSON content type.

REST Assured has a patch() method next to get(), post() and put(), and the rest of the chain is identical: content type, body, path. Because the reply is the full record, one response lets you check both halves of the contract, the field you changed and the fields you did not touch.

About the assignment

Move user 1 to a new city with a one-field PATCH and prove from the response that the name survived the update unchanged.

What you will do

  1. Prepare the request with given(), the JSON content type and a body holding just the new city from the starter.
  2. Send it with patch() to /users/1.
  3. Print the status code after Status:.
  4. Print the name field after Name: and the city field after City:, both read from the same response.

Common mistakes

  • Using put() by habit

    The same one-field body sent with put() is rejected with 400 because PUT requires name and email. The verb is the whole point of this lesson.

  • Patching with an empty object

    Sending {} to see what happens returns 400 with a message asking for fields to update. A PATCH needs at least one field.

  • Only checking the changed field

    Asserting that city changed proves half the behaviour. The bug PATCH tests exist to catch is a server that wipes the other fields, so assert on an untouched one such as name too.

In a real test suite

Partial updates show up in profile screens, status toggles and admin tools. In a JUnit suite a PATCH test usually asserts with body("city", equalTo(...)) and body("name", equalTo(...)) in one then() chain, so a single run checks both the change and the preservation.

To try one of these calls on any API endpoint without a task, open the Online Selenium Java Compiler (REST Assured is included).

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import io.restassured.http.ContentType;
import static io.restassured.RestAssured.*;

public class Main {
    public static void main(String[] args) {
        RestAssured.baseURI = "https://testkru.com/api";

        // TODO: PATCH /users/1 with body: {"city": "San Francisco"}
        // TODO: Print status, name (should still be "John Doe"), and city (should be updated)
    }
}