Skip to content
CodeKruPro

Send POJO as Request Body

44/62

Send POJO as Request Body

Instead of constructing JSON strings manually, you can send a Java object (POJO) directly. REST Assured automatically serializes it to JSON:

User user = new User();
user.name = "Test";
given().contentType(ContentType.JSON).body(user).post("/users");

Define the class with public fields inside your Main class as static. Every public field is sent, so set all the fields the endpoint requires: /users answers 400 if name or email is missing, and a field you never set goes out as null (or 0 for an int).

Task: Create a User POJO and POST it.

Expected output:

Status: 201
Name: Test User
City: Mumbai

About this lesson

REST Assured POJO request body: serialization of a Java object to JSON

Learn REST AssuredAdvancedSerialization and Deserialization

How it works

Writing JSON by hand inside Java strings is fragile: one missing quote or escaped character and the payload is broken. REST Assured can instead take a plain Java object for body() and serialize it for you. In this runtime it uses Jackson, which writes every public field as a JSON key named exactly like the field.

Unset fields are still written. A String you never assigned becomes null in the JSON, and an int becomes 0. For testkru POST /users, which requires name and email, a forgotten email therefore leads to a 400 with an error naming the missing field, while a forgotten city is accepted and echoed back as null.

The content type decides whether the server reads the body at all. Without contentType(ContentType.JSON), REST Assured still writes the object as JSON text but labels the request as text/plain with an ISO-8859-1 charset. This API only parses bodies marked as JSON, so it sees no fields and answers 400 for both name and email. With the JSON content type set, the user is accepted with status 201 and returned with an id added.

About the assignment

Fill a User object with the values from the starter comment, send it as the body of a POST, and report the status plus the name and city the server returned.

What you will do

  1. Create a User with new User() and assign all four public fields: name, email, age and city.
  2. Start from given(), set contentType() to ContentType.JSON, and pass the object to body().
  3. POST to /users and keep the response.
  4. Print the status code after Status:, then the returned name and city after Name: and City:.

Common mistakes

  • Leaving out the content type

    The object is serialized, but the request goes out as plain text, the server ignores it and the reply is 400. Set the JSON content type whenever you send an object.

  • Making the fields private without getters

    Jackson writes public fields and properties exposed through getters. A class with only private fields and no getters gives it no properties at all, and serialization fails with an InvalidDefinitionException saying no serializer was found. Keep the fields public as in the starter, or add getters.

  • Using a primitive for an optional number

    An int field can never be absent: an unset age is sent as 0, which an API may store as a real age. Use Integer when a value is optional so that it serializes as null instead.

In a real test suite

Real suites keep request models in their own package, often with a builder or factory that fills sensible defaults so each test only sets the field it is about. The same classes double as response models, so a test can send an object and then compare what came back field by field.

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 {

    static class User {
        public String name;
        public String email;
        public int age;
        public String city;
    }

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

        // TODO: Create a User object with:
        //   name = "Test User", email = "test@example.com", age = 28, city = "Mumbai"
        // TODO: POST to /users with the User object as the body
        // TODO: Print status, name, and city from the response
    }
}