Filtering request and response .
We know we can log the request we sent to a server and even the response we get back like this
Response response = given().contentType(ContentType.JSON).header("X","Y").log().all().get();
And we can even log response like this
given().contentType(ContentType.JSON).get("/list").then().log().body();
But the above two methods just log the request and response that is print them to the console. But what if we want to make some use of the request we sent and the response we get .
We can store our response in a String like this
String response = given().contentType(ContentType.JSON).log().all().get(“/list”).toString();
But filters offer a better way of filtering and storing requests and responses.So if we want to store them in variables and use later on.we use filters.Infact filters are the preferred methods. Even to print request/response to console we should use filters as they are better than logging.
For filters we need to define objects like requestwriter ,requestcapture,printwriter,printcapture and initiate them in before test and then use them on our request to get request and response that we can print.
Below is a get request and response to get data from server.
import io.restassured.RestAssured;
import io.restassured.filter.log.RequestLoggingFilter;
import io.restassured.filter.log.ResponseLoggingFilter;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
public class FiltersExample {
public static StringWriter requestWriter;
public static PrintStream requestCapture;
public static StringWriter responseWriter;
public static PrintStream responseCapture;
@BeforeClass
public void beforeEachMethod(){
requestWriter = new StringWriter();
requestCapture = new PrintStream(new WriterOutputStream(requestWriter),true);
responseWriter = new StringWriter();
responseCapture = new PrintStream(new WriterOutputStream(responseWriter),true);
}
@Test
public void getAllStudentsData(){
RestAssured.baseURI = "http://localhost";
RestAssured.port = 8080;
RestAssured.basePath = "/student";
given().filter(new RequestLoggingFilter(requestCapture)).filter(new ResponseLoggingFilter(responseCapture)).when().get("/10");
String request = requestWriter.toString();
System.out.println(request);
System.out.println("------------------------------------------------------------");
String response = responseWriter.toString();
System.out.println(response);
}
In below example we try to write/post data to server and we filter the req we are sending and response we will get from server.
@Test
public void getStudentData() throws JSONException{
RestAssured.baseURI = "http://localhost";
RestAssured.port = 8080;
RestAssured.basePath = "/student";
JSONObject obj = new JSONObject();
obj.put("firstName","H");
obj.put("latsName", "S");
obj.put("email", "hsd@example.com");
obj.put("programme", "CS");
JSONArray arr = new JSONArray();
arr.put("Java");
arr.put("c");
arr.put("c++");
obj.put("courses", arr);
given().filter(new RequestLoggingFilter(requestCapture)).filter(new ResponseLoggingFilter(responseCapture)).body(obj.toString()).when().post();
String request = requestWriter.toString();
System.out.println(request);
System.out.println("------------------------------------------------------------");
String response = responseWriter.toString();
System.out.println(response);
System.out.println("------------------------------------------------------------");
}