Request logging in REST Assured
In this post i'll explain how to capture API request while writing the API tests. Before we jump in, lets first understand the importance of request/response logging in API automation.
When it comes to API automation, most of the time i have seen people just logging the API response in test reports but we need to understand its equally important to the log the request as well. Why?
In a happy scenario nobody cares about the request you hit but as soon as you report API test failure, developer will definitely ask about the request you made. Most of the time they think tester might have hit the request incorrectly.
Some developers might doubt your automation script before they look at their code. In such cases you can always show them the request and match it with the contract that you are following. Even you can use the request in postman and check the response to make sure nothing is wrong with your code.
It can make debugging very easy in case of failure: When API test fails you can directly see if something was wrong with the request in terms of (endpoint, headers, queryparams etc)
It helps in bug logging. Attach the request/response in the bug.
Its always easy to visualise the request/response in test reports instead of fetching it from your code or printing it on console.
#Show me the code
I am demonstrating this example with a sample API GET request using REST Assured.
Maven dependencies (REST Assured and Apache commons library)
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
Sample class
public class RequestCapturingExample {
public static void main(String[] args) {
RequestCapturingExample requestCapturingExample = new RequestCapturingExample();
requestCapturingExample.getRequest();
}
private void getRequest() {
StringWriter requestWriter = new StringWriter();
PrintStream requestCapture = new PrintStream(new WriterOutputStream(requestWriter), true);
Response response = given()
.filter(new RequestLoggingFilter(requestCapture))
.get("http://dummy.restapiexample.com/api/v1/employees");
System.out.println(requestWriter.toString());
System.out.println(response.asString());
}
}
Code explanation:
Here we are using REST Assured as a client to hit a GET API. The sample test APIs that i am using can be found here
First we need to create the object of PrintStream class using the StringWriter object
StringWriter requestWriter = new StringWriter(); PrintStream requestCapture = new PrintStream(new WriterOutputStream(requestWriter), true);
Now we will use requestCapture object to capture the request using the filter method while hitting the GET API
Response response = given()
.filter(new RequestLoggingFilter(requestCapture))
.get("http://dummy.restapiexample.com/api/v1/employees");
Then the request/response can be printed using the requestWriter and response objects respectively. These objects can be further used to log or attach the request/response in test reports.
System.out.println(requestWriter.toString()); System.out.println(response.asString());
You request/response will look like something below
Here you can see all the details in the API request ranging from endpoint, headers, queryparams, formparams, cookies etc. Let me know in case you face any difficulty while understanding or implementing this.
Thanks,
Nitin Goyal
Rest assured with allure is really simple to implement and there is also option to integrate with cucumber
Good one. If people don’t want to take headache of explicit logging then they can use Serenity BDD which gives beautiful report.