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
- Build the request with
given(), the JSON content type and the four-field body from the comment. - Send it with
put()to/users/1and keep theResponse. - Show the HTTP status next to
Status:. - Read
name,emailandcitywithgetString()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
ageandcityhere and they come backnull: 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
nameandemailas 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.
Related lessons
- Partial Update with PATCHChanges one field of the same user with PATCH and keeps the rest.
- Handle PUT Validation ErrorLeaves the required email out of a PUT and reads the 400 error.
- Create a User with POSTCreates a user with POST, the request that comes before any update.
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.
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
}
}