REST Assured: how do I POST JSON and assert the response?
Basic pattern for a POST /users with a JSON body and status + body assertions.
rest-assuredapijava
Basic pattern for a POST /users with a JSON body and status + body assertions.
Accepted solution
Set the content type, pass a body (POJO, Map or raw string) and assert on the response.
Order order = new Order("SKU-42", 2);
given()
.contentType(ContentType.JSON)
.header("Authorization", "Bearer " + token)
.body(order) // serialised by Jackson/Gson on the classpath
.when()
.post("/api/orders")
.then()
.statusCode(201)
.body("id", notNullValue())
.body("items[0].sku", equalTo("SKU-42"))
.time(lessThan(2000L));Common gotchas: without `contentType(JSON)` the server sees `text/plain` and returns 415. Serialization needs Jackson or Gson on the classpath — otherwise pass a raw JSON string or a `Map`. For contract-level confidence add `.body(matchesJsonSchemaInClasspath("order-schema.json"))` so a renamed field fails the test instead of silently returning null.