Skip to content
CodeKruPro

Full Update with PUT

20/62

Full Update with PUT

PUT replaces the entire resource. You must send all required fields.

Task:

  1. PUT to /users/1 with a complete replacement body
  2. Print the updated fields

Expected output:

Status: 200
Name: Updated John
Email: john.updated@example.com
City: San Francisco

About this lesson

REST Assured PUT request example: replace a whole user resource

Learn REST AssuredIntermediatePUT and PATCH

How it works

HTTP PUT means "store this representation at this URI". The body is the complete new state of the resource, not a list of changes. A server that follows those semantics replaces every field, so anything you leave out is reset rather than kept.

The testkru users API behaves exactly like that. PUT /users/1 with only a name and an email returns 200 with age and city set to null, because the old values of user 1 were not part of the new representation. The id comes from the path, not from the body. An id outside 1 to 5 gets 404 before the body is even looked at.

In REST Assured the call shape is the same as a POST: given(), a JSON content type, body(), then put() with the path. The response is the updated resource, so you can read it with JsonPath straight away. The sandbox does not persist writes, so a later GET /users/1 still shows John Doe from New York.

About the assignment

Replace user 1 with the full record given in the starter comment, all four fields, and print the status and three of the fields the server returns.

What you will do

  1. Build the request with given(), the JSON content type and the four-field body from the comment.
  2. Send it with put() to /users/1 and keep the Response.
  3. Show the HTTP status next to Status:.
  4. Read name, email and city with getString() and print each after its own label.

Common mistakes

  • Sending only the changed field

    Treating PUT like a partial update is the classic bug. Leave out age and city here and they come back null: the server did what PUT asks, which was to replace the record.

  • Forgetting contentType()

    Without it the String body is sent as plain text. The endpoint sees no JSON, reports both name and email as missing and returns 400.

  • Expecting 201 from an update

    This API answers a successful PUT on an existing user with 200 and the new record. 201 Created belongs to the POST in lesson 6. Assert the status the contract promises, not the one you guess.

In a real test suite

Update tests usually build the body from a POJO or a Map so every field is always present, then assert on the returned record with body() matchers. A follow-up GET confirms the write was stored, which on a real backend is the step that catches lost updates.

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: PUT to /users/1 with body:
        // {"name": "Updated John", "email": "john.updated@example.com", "age": 31, "city": "San Francisco"}
        // TODO: Print status, name, email, city
    }
}