how to logging web api code example
Example: logging in api
Logging
In many cases it can be useful to print the
response and/or request details in order to
help you create the correct expectations and
send the correct requests. To do help you do thi
s you can use one of the predefined filters
supplied with REST Assured or you can use one of the shortcuts.
Request Logging
given().log().all(). ..
Log all request specification details
including parameters, headers and body
given().log().params(). ..
given().log().body(). ..
given().log().headers(). ..
given().log().cookies(). ..
given().log().method(). ..
given().log().path(). ..
Response Logging
If you want to print the response body regardless of the
status code you can do:
get("/x").then().log().body() ..
This will print the response body regardless if an error occurred.
If you're only interested in printing the response body if an error
occur then you can use:
get("/x").then().log().ifError(). ..
You can also log all details in the response
including status line, headers and cookies:
get("/x").then().log().all(). ..
as well as only status line, headers or cookies:
get("/x").then().log().statusLine(). ..
get("/x").then().log().headers(). ..
get("/x").then().log().cookies(). ..
You can also configure to log the response
only if the status code matches some value:
get("/x").then().log().ifStatusCodeIsEqualTo(302). ..
Only log if the status code is equal to 302
get("/x").then().log().ifStatusCodeMatches(matcher). ..
Only log if the status code matches the supplied Hamcrest matcher