← Back to libraryQuestion 235 of 468
🔌REST AssuredIntermediate

Sending a Request Body (String, Map, or POJO)

📌 Definition:

REST Assured accepts a request body as a raw JSON String, a Java Map/List, or a POJO (which it serializes to JSON automatically via Jackson/Gson). Set contentType so the server interprets it correctly.

📖 Detailed Explanation:

You can pass .body("{\"name\":\"QA\"}") (raw string), .body(mapOfFields) (a Map serialized to JSON), or .body(userPojo) (a Java object serialized by Jackson/Gson on the classpath). POJO bodies are preferred for maintainability and type safety — you build a User object and REST Assured converts it. contentType(JSON) ensures Content-Type is set. For form submissions use formParam or contentType(URLENC). Building bodies from POJOs/Maps avoids brittle escaped JSON strings and keeps test data structured and reusable.

🔑 Key Points:
  • Body can be a JSON String, Map/List, or POJO
  • POJO/Map serialized to JSON by Jackson/Gson (preferred)
  • Set contentType(JSON) so the body is interpreted correctly
  • Avoid brittle escaped JSON strings
🌍 Real-World Example:

Creating a user from a POJO: User u = new User('QA', 'qa@test.com'); given().contentType(JSON).body(u).when().post('/users') — Jackson serializes u to JSON automatically.

🎯 Scenario-Based Interview Question:

Why is given().body(userPojo) preferred over given().body("{ big escaped JSON string }")?