Rest API Notes
Rest API Notes
import io.restassured.RestAssured;
import io.restassured.response.Response;
.header("Content-Type", "application/json")
.when()
.get("https://api.example.com/users")
.then()
.statusCode(200)
.extract().response();
✅ 3. HTTP Methods:
Java
// GET
get("/users");
// POST
given().body(jsonData).post("/users");
// PUT
given().body(updatedData).put("/users/1");
// DELETE
delete("/users/1");
✅ 4. Validating Response:
Java
.then().statusCode(200)
.body("data.id", equalTo(1));
given().log().all();
then().log().all();
// Path
.get("/users/{id}", 2);
// Query
.queryParam("page", 2)
.get("/users");
✅ 7. Authentication:
Java
// Basic
.auth().basic("username", "password");
// OAuth2
.auth().oauth2("token");
✅ 8. JSON/XML Parsing:
Java
response.jsonPath().get("data.name");
response.xmlPath().get("response.user.name");
✅ 9. Schema Validation:
Java
.then().body(JsonSchemaValidator.matchesJsonSchemaInClasspath("sc
hema.json"));
// POJO to JSON
// JSON to POJO
● Client-Server
● Cacheable
● Uniform Interface
● Layered System
Example:
Java
.header("If-None-Match", etagValue)
Example:
Java
stubFor(get(urlEqualTo("/users/1"))
.willReturn(aResponse().withStatus(200).withBody("{\"name\":\"Tes
t\"}")));
✅ 14. Response Time Validation:
Java
.then().time(lessThan(2000L));
Unset
<test name="Test1">
<packages>
</packages>
</test>
<test name="Test2">
<packages>
</packages>
</test>
</suite>
➤ By Instances (Classes):
Unset
<classes>
</classes>
</test>
</suite>
.setBaseUri("https://api.example.com")
.setContentType(ContentType.JSON)
.build();
.expectStatusCode(200)
.expectContentType(ContentType.JSON)
.build();
// Usage:
given().spec(requestSpec)
.when().get("/users")
.then().spec(responseSpec);
params.put("userId", 1);
params.put("type", "active");
given().params(params).get("/users");
Java
if(response.statusCode() == 429) {
Thread.sleep(5000);
// Retry the request
given()
.when().get("/users")
.then().assertThat().statusCode(200);
Java
given()
.multiPart("file", file)
.when()
.post("/upload")
.then().statusCode(200);
➤ File Download:
Java
.when()
.get("/download/sample.pdf");
Files.write(Paths.get("downloaded_sample.pdf"), fileData);
given()
.formParam("username", "admin")
.formParam("password", "pass123")
.when()
.post("/login")
.then().statusCode(200);
● Downstream API: API that depends on your service. It calls your API.
Interview Context:
● How do you mock an upstream API?
✅
This doc gives you 100% coverage for REST Assured-based interview preparation. Use it as
your quick revision go-to guide before interviews.