Skip to content
CodeKruPro

Validate 201 Created

6/62

Validate 201 Created

201 Created is returned when a new resource is successfully created via POST.

The /users endpoint accepts POST requests with a JSON body containing name and email. It returns the created user with id: 6.

Task:

  1. POST to /users with a JSON body: {"name": "Test User", "email": "test@example.com"}
  2. Validate the status code is 201
  3. Print the status, created ID, and name

Expected output:

Status: 201
Created ID: 6
Name: Test User

About this lesson

REST Assured POST request returning 201 Created

Learn REST AssuredBeginnerStatus Code Validation

How it works

POST asks the server to create something from the body you send. When it works, the conventional answer is 201 Created rather than 200, often with the new record in the body. For user creation the practice API answers with the stored fields plus a fixed new id, and the status line reads HTTP/1.1 201 Created.

A POST has two parts REST Assured must know about: the body text, set with body(), and its media type, set with contentType(). The API only parses the body when the Content-Type header says JSON. Without that call, REST Assured labels a String body as text/plain with an ISO-8859-1 charset, and the server sees no fields at all.

The created user is not saved: a follow-up GET for the new id still returns 404. That keeps every run predictable.

About the assignment

You create a test user by POSTing a small JSON document with a name and an email, check that the server confirms the creation with 201, and print the status together with the id and name from the reply.

What you will do

  1. Start with given() and set the content type to application/json with contentType().
  2. Pass the JSON text from the task to body(), escaping its inner double quotes.
  3. Send it with post() to /users and store the Response.
  4. Confirm the status is 201, then read id and name from the body.
  5. Print the status, the id and the name on three labelled lines.

Common mistakes

  • Leaving out contentType()

    Without it the body goes out as plain text, the API cannot read it, and you get 400 with a message that both name and email are missing, even though they are in your string.

  • Omitting a required field

    Name and email are both required. Leave out the email and the reply is 400 naming exactly that field, so a typo in a key name looks the same as a missing one.

  • Expecting 200 for a create

    A test that asserts statusCode(200) after this POST fails, because creation answers 201. Assert the code the API promises for that operation, not the generic success code.

In a real test suite

Teams rarely keep JSON in escaped string literals. They build the body from a Map or a small Java class and let REST Assured serialise it, assert 201 together with the returned fields, and keep the new id for the follow-up requests of the same scenario.

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 static io.restassured.RestAssured.*;

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

        // TODO: POST to "/users" with Content-Type "application/json"
        //       and body: {"name": "Test User", "email": "test@example.com"}
        // HINT: Use .contentType("application/json").body("...")
        // TODO: Print status, created ID, and name
    }
}