The way we logged our get or post request we can even log the response we get from server. Only difference is for logging a request we have to put log before the get request like log.header.getor log.body.post
In case of logging the reponse we put log methods after get or post method which we see below. We put the log method after then() . That is log after we got result from server
package logging;
import static io.restassured.RestAssured.given;
import java.io.File;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
public class ResponseLoggingExamples {
@Test
public void getHeaderLogs(){
RestAssured.baseURI = "http://localhost";
RestAssured.port = 8080;
RestAssured.basePath = "/student";
System.out.println("--------------Log headers from response for get request---------------");
given().contentType(ContentType.JSON).get("/18").then().log().headers();
System.out.println("--------------Log body from response for get request---------------");
given().contentType(ContentType.JSON).get("/list").then().log().body();
//Log if there is an error
given().contentType(ContentType.JSON).get("/list").then().log().ifError();
File jsonBody = new File("C:\\workspace\\RestAssuredProject\\students.json");
//Log header from response from post request
// given().contentType(ContentType.JSON).body(jsonBody).post().then().log().headers();
//Log body from response from post request
given().contentType(ContentType.JSON).body(jsonBody).post().then().log().body();
}
}