Skip to content
CodeKruPro

Handle PUT Validation Error

22/62

Handle PUT Validation Error

Since PUT replaces the entire resource, omitting required fields causes a 400 Bad Request.

Task:

  1. PUT to /users/1 with only {"name": "Test"} (missing required email)
  2. Print the status and error message

Expected output:

Status: 400
Error: Missing required field(s): email. Required fields: name, email

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

  1. Build a JSON request whose body contains only the name field from the starter comment.
  2. Send it with put() to /users/1.
  3. Print the status code after Status:.
  4. Read the error field with getString() and print it after Error:.

Common mistakes

  • Dropping the content type

    Without a JSON content type the body is not parsed, so the server reports name as 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 use error. Asking for message returns null with 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.

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 only {"name": "Test"} (missing email)
        // TODO: Print the status code and error message
    }
}