← Back to libraryQuestion 231 of 468
🔌REST AssuredBeginner

Making Requests — GET, POST, PUT, DELETE

📌 Definition:

REST Assured sends HTTP requests with methods matching the verb: .get(), .post(), .put(), .patch(), .delete(), each taking the endpoint path. Request configuration comes from the preceding given() block.

📖 Detailed Explanation:

GET retrieves and usually needs only params/headers; POST creates and needs a body and contentType; PUT/PATCH update (PUT replaces, PATCH partially updates) and need a body; DELETE removes a resource. You set the body with .body(...) and content type with .contentType(ContentType.JSON). The endpoint can include path templates (.get('/users/{id}', 1)). REST Assured returns a Response you can validate in then() or capture. Matching the correct verb and body expectations to the API contract is fundamental — sending POST without a body/contentType, or using PUT where PATCH is expected, are common test bugs.

🔑 Key Points:
  • .get/.post/.put/.patch/.delete(endpoint) map to HTTP verbs
  • POST/PUT/PATCH need .body(...) + .contentType(JSON)
  • PUT replaces the resource; PATCH updates part of it
  • Path templates: .get('/users/{id}', id)
🌍 Real-World Example:

Full CRUD in tests: POST /users (201, body created), GET /users/{id} (200, verify fields), PUT /users/{id} (200, updated), DELETE /users/{id} (204) — each verb exercising a lifecycle stage.

🎯 Scenario-Based Interview Question:

A POST test fails with 415 Unsupported Media Type even though the JSON body is correct. What's the likely cause?