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
- Start with
given()and set the content type toapplication/jsonwithcontentType(). - Pass the JSON text from the task to
body(), escaping its inner double quotes. - Send it with
post()to/usersand store theResponse. - Confirm the status is 201, then read
idandnamefrom the body. - 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.
Related lessons
- Create a User with POSTA fuller POST with four fields and the
ContentType.JSONconstant. - Echo Request BodySee your request body mirrored back to check what was really sent.
- Send POJO as Request BodySend a Java object as the POST body instead of a JSON 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 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
}
}