REST Assured Log if validation fails

Profile picture for user devraj

Since REST Assured 2.3.1 you can log the request or response only if the validation fails.

Log Request

// To log all Request information
given().log().ifValidationFails()

// To log specific request information, like in below to log Request Method
given().log().ifValidationFails(LogDetail.METHOD)

Log Response

// Log all the response information
.log().ifValidationFails()

// Log specific response information, like body in below example
.log().ifValidationFails(LogDetail.BODY)

// Log specific response information, like body in below example
// Set true to pretty-print 
.log().ifValidationFails(LogDetail.BODY, true)

Log Request and Response Together

// Log all detail
given().config(RestAssured.config().logConfig(LogConfig.logConfig().enableLoggingOfRequestAndResponseIfValidationFails()))

// Log only header or other specific detail
given().config(RestAssured.config().logConfig(LogConfig.logConfig().enableLoggingOfRequestAndResponseIfValidationFails(LogDetail.HEADERS)))

Log Request and Response Together - Shortcut

Use it outside given()

// To log everything
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();

// To log request and response header
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(LogDetail.HEADERS);