In the same way we can log paramters or log body from a get or post request . We can even log only if status code is not equal to the value we wanted.
//Log the parameters example
@Test
public void getParameterLogs(){
RestAssured.baseURI = "http://localhost";
RestAssured.port = 8080;
RestAssured.basePath = "/student";
Response response = given().param("programme","computer science").log().params().get("/list");
response.prettyPrint();
}
//Log the body example
@Test
public void getBodyLogs(){
RestAssured.baseURI = "http://localhost";
RestAssured.port = 8080;
RestAssured.basePath = "/student";
File jsonBody = new File("C:\\workspace\\RestAssuredProject\\students.json");
given().contentType(ContentType.JSON).log().body().body(jsonBody).post();
}
@Test
public void assertValidations(){
RestAssured.baseURI = "http://localhost";
RestAssured.port = 8080;
RestAssured.basePath = "/student";
System.out.println("---------------Asserting that status code is 200 ------------");
File jsonBody = new File("C:\\workspace\\RestAssuredProject\\students.json");
//then() method is used when we want to put some validation on a req.so below we post a body i.e body(jsonBody) to the server and
//then we used then() method to put validation after that .so we put statusCode(500) and before body we also put log.ifValidationFails
//That means this test will only log if validation fails i.e only if status code is not 200
given().contentType(ContentType.JSON).log().ifValidationFails().body(jsonBody).post().then().statusCode(200);
}