POST creates new resources. Unlike GET where you just read data, POST sends a request body. REST Assured gives you three ways to build that body: raw String, HashMap, or POJO. Each has its place.
The simplest way. Write the JSON as a String. Quick and dirty. Good for one-off tests.
@Test
public void testCreatePostWithString() {
String requestBody = """
{
"title": "REST Assured is awesome",
"body": "Learning API automation with Java",
"userId": 1
}
""";
given()
.contentType(ContentType.JSON)
.body(requestBody)
.when()
.post("/posts")
.then()
.statusCode(201)
.body("title", equalTo("REST Assured is awesome"))
.body("id", notNullValue()); // Server assigns an ID
}The triple-quote """ syntax (text blocks) requires Java 15+. If you are on an older Java version, use regular string concatenation or escape the quotes. Most projects use Java 17 or higher now, so text blocks should work fine.
Build the JSON from a HashMap. No string escaping. No worrying about commas and quotes. REST Assured converts the Map to JSON automatically.
import java.util.HashMap;
import java.util.Map;
@Test
public void testCreatePostWithMap() {
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("title", "Created with HashMap");
requestBody.put("body", "This is cleaner than raw strings");
requestBody.put("userId", 1);
given()
.contentType(ContentType.JSON)
.body(requestBody) // REST Assured converts Map to JSON
.when()
.post("/posts")
.then()
.statusCode(201)
.body("title", equalTo("Created with HashMap"));
}The professional way. Create a Java class (POJO) that mirrors the JSON structure. REST Assured + Jackson converts it to JSON. This is what real frameworks use. Type-safe. Reusable. Maintainable.
// src/test/java/com/yourcompany/pojo/Post.java
public class Post {
private int userId;
private String title;
private String body;
private int id; // Set by server
// Default constructor (Jackson needs this)
public Post() {}
// Constructor for creating new posts
public Post(int userId, String title, String body) {
this.userId = userId;
this.title = title;
this.body = body;
}
// Getters and Setters
public int getUserId() { return userId; }
public void setUserId(int userId) { this.userId = userId; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public String getBody() { return body; }
public void setBody(String body) { this.body = body; }
public int getId() { return id; }
public void setId(int id) { this.id = id; }
}@Test
public void testCreatePostWithPojo() {
Post newPost = new Post(1, "Created with POJO", "Type-safe and clean");
given()
.contentType(ContentType.JSON)
.body(newPost) // Jackson converts POJO to JSON
.when()
.post("/posts")
.then()
.statusCode(201)
.body("title", equalTo("Created with POJO"));
}Always set contentType(ContentType.JSON) when sending a body. Without it, the server might not understand your request. This is equivalent to the Content-Type: application/json header you set in Postman.
@Test
public void testCreateAndVerify() {
// Step 1: Create
int newPostId =
given()
.contentType(ContentType.JSON)
.body(new Post(1, "Verify Me", "Check if created"))
.when()
.post("/posts")
.then()
.statusCode(201)
.extract()
.jsonPath().getInt("id"); // Extract the ID
System.out.println("Created post with ID: " + newPostId);
// Step 2: Read back (verify it exists)
given()
.pathParam("id", newPostId)
.when()
.get("/posts/{id}")
.then()
.statusCode(200)
.body("title", equalTo("Verify Me"));
}Q: What are the different ways to send a POST request body in REST Assured?
A: Three ways: 1) String — pass a raw JSON string to .body(). Quick for simple tests but error-prone. 2) HashMap — create a Map with key-value pairs and pass it to .body(). REST Assured auto-converts it to JSON. No string escaping issues. 3) POJO — create a Java class with fields matching the JSON structure, add getters/setters, and pass the object to .body(). Jackson serializes it to JSON. This is the best practice for real frameworks because it is type-safe, reusable, and maintainable. Always set contentType(ContentType.JSON) when sending JSON bodies.
Key Point: Three ways to send a body: String (quick), HashMap (clean), POJO (professional). Always set contentType(ContentType.JSON). Extract the created resource ID with .extract().jsonPath().getInt("id").