About this lesson
REST Assured POST JSON body with ContentType.JSON
Learn REST AssuredIntermediatePOST Requests
How it works
io.restassured.http.ContentType is an enum of common media types. Passing ContentType.JSON to contentType() sets the request header to application/json, the signal this API needs before it parses the body. It is safer than typing the string yourself, because a typo in a constant name fails at compile time.
The JSON itself can be a String or a Java structure. A String is sent as written, so it must be valid JSON with double quotes. A Map passed to body() is turned into JSON for you, keys and values included. Java 21 also supports text blocks, three double quotes on each side, which let you paste JSON without escaping.
The API echoes your types back: send age as a number and you get an Integer, send it quoted and you get the string.
About the assignment
You create a fuller user with four fields: name, email, age and city. After the POST you print the status and three values from the created record, which carries the new id the API assigns.
What you will do
- Start with
given()and set the content type withcontentType()and theContentType.JSONconstant. - Put the four-field JSON from the starter comment in
body(), as an escaped string or a text block. - Send it with
post()to/usersand keep theResponse. - Read
idwithgetInt()andnameandcitywithgetString(). - Print status, id, name and city on four labelled lines.
Common mistakes
Single quotes inside the JSON
JSON only allows double quotes. A body written with single quotes is invalid, the API cannot parse it, and you get 400 saying both name and email are missing.
Quoting numbers by accident
Sending the age in quotes still returns 201, but the record now holds the string 25, and a numeric matcher such as
equalTo()with the number 25 fails against it.Expecting optional fields to appear
Fields you leave out, such as city, come back as JSON
null. A test that prints them will show the word null, not an error.
In a real test suite
String bodies are fine for a lesson, but suites usually build payloads from a Map or a POJO so values can be changed per test. JUnit 5 parameterised tests then reuse one POST method with different inputs, asserting 201 for valid data and 400 for each missing field.
Related lessons
- Validate 201 CreatedThe minimal POST with a two-field body and the 201 check.
- Send POJO as Request BodySend the user as a Java object and let Jackson write the JSON.
- Validate JSON InputPOST to a validation endpoint that reports whether the JSON is valid.
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: POST to /users with ContentType.JSON
// Body: {"name": "New User", "email": "new@example.com", "age": 25, "city": "Mumbai"}
// TODO: Print status, id, name, and city
}
}