About this lesson
REST Assured PUT with a missing required field: read the 400 error message
Learn REST AssuredIntermediatePUT and PATCH
How it works
Because PUT replaces the whole resource, the server has to insist on every field it cannot invent. For testkru users those are name and email. Leave one out and the request is answered with 400 Bad Request before anything is stored, and the JSON body explains which field was missing and which fields are required.
Most error bodies on this API share one shape: a string error and a numeric status that repeats the HTTP code. So the same JsonPath call reads the message whether it came from a missing user, a bad PUT or a failed login. The message is built from the fields that failed, so it names both if you send a body the server cannot parse at all.
REST Assured hands you the 400 response like any other. The difference shows up only when you assert: then().statusCode(200) on this request throws an AssertionError that reports the expected code 200 against the actual 400, which is the kind of failure message you want in a CI log.
About the assignment
Send user 1 a PUT whose body has a name but no email, and print the status and the server's explanation of what went wrong.
What you will do
- Build a JSON request whose body contains only the
namefield from the starter comment. - Send it with
put()to/users/1. - Print the status code after
Status:. - Read the
errorfield withgetString()and print it afterError:.
Common mistakes
Dropping the content type
Without a JSON content type the body is not parsed, so the server reports
nameas missing too. The test still gets 400, but for a different reason than the one you meant to test.Looking for a message field
Success responses on other endpoints use
message, but errors here useerror. Asking formessagereturnsnullwith no warning.Using a user id that does not exist
With an id such as 99 the server answers 404 before it reads the body. A validation test needs an existing resource, otherwise you are testing the lookup instead.
In a real test suite
Contract tests for update endpoints normally cover each required field on its own: drop one, expect 400, and assert with containsString() that the message names that field. Keeping the assertion loose on wording and strict on the field name makes the test stable.
Related lessons
- Full Update with PUTThe same PUT with every field present, returning 200.
- Handle 404 Not FoundReads the
errorfield from a 404 for an unknown user id. - Handle Validation ErrorsAnother 400, this time with an array of errors instead of one string.
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 only {"name": "Test"} (missing email)
// TODO: Print the status code and error message
}
}