REST Assured Tutorial 68 – Request Logging In Rest Assured

As a part of End to End REST Assured Tutorial , in this post We will learn :- How to log request specification in Rest Assured?

When a request is sent to server, it consists many details like headers, cookies, body if any etc. When we get response from server, that also include headers, response body if any etc. We may need to log these information of request and response to know if correct headers, param etc are sent and received.

Rest Assured supports this logging feature. In this post, we will learn about logging Request specification.

We can log Request Specification before it is sent to server for processing. Rest Assured supports logging of Request Specification using RequestLoggingFilter class. This filter only log what is passed in Request Specification. Addition data with request like headers etc are also attached with request by HTTP Builder and HTTP Client. Those details will not be logged by Rest Assured.

RequestSpecification interface provides a method named log() that helps you to log everything or specific part of Request Specification. We have below methods of logging for Request specification:-

all() – Logs everything in the specification, including e.g. headers, cookies, body with the option to pretty-print the body if the content-type is either XML, JSON or HTML, Same job is done by a method named “everything()”.

body() – Logs only the content of the body. The body will be pretty-printed by default if content-type is either XML, JSON or HTML.

headers() – Logs only the headers.

cookies() – Logs only the cookies.

method() – Logs only request method i.e. POST, GET etc.

params() or parameters() – Logs only the parameters of the request.  It will print all query params, form params, request params and path params.

uri()– Only logs the request uri.

We will see an example:-

For demo purpose, I will add dummy headers, params and cookies. It will make API to work unexpectedly.

Rest Assured Program:-

Detailed Program:-

package LoggingInRestAssured;

import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.specification.RequestSpecification;

public class LogAllExampleDetailed {

	@Test
	public void logAll() {
		
		// There is no need to add escape character manually. Just paste string within double 
		// quotes. It will automatically add escape sequence as required. 
		String jsonString = "{\r\n" + 
				"    \"firstname\" : \"Amod\",\r\n" + 
				"    \"lastname\" : \"Mahajan\",\r\n" + 
				"    \"totalprice\" : 121,\r\n" + 
				"    \"depositpaid\" : true,\r\n" + 
				"    \"bookingdates\" : {\r\n" + 
				"        \"checkin\" : \"2020-02-01\",\r\n" + 
				"        \"checkout\" : \"2020-02-05\"\r\n" + 
				"    },\r\n" + 
				"    \"additionalneeds\" : \"Breakfast\"\r\n" + 
				"}";
		
		// Creating an object of RequestSpecBuilder
		RequestSpecBuilder reqBuilder = new RequestSpecBuilder();
		// Setting Base URI
		reqBuilder.setBaseUri("https://restful-booker.herokuapp.com");
		// Setting Base Path
		reqBuilder.setBasePath("/booking");
		// Setting demo headers
		reqBuilder.addHeader("DemoHeader1", "Headervalue1");
		reqBuilder.addHeader("DemoHeader2", "Headervalue2");
		// Setting demo cookies
		reqBuilder.addCookie("DemoCookie1", "DemoCookieValue1");
		reqBuilder.addCookie("DemoCookie2", "DemoCookieValue2");
		// Setting demo params
		reqBuilder.addParam("demoParam1", "demoParam1value");
		reqBuilder.addParam("demoParam2", "demoParam2value");
		// Setting demo path params
		reqBuilder.addPathParam("demoPathParam1", "demoPathParam1value");
		reqBuilder.addPathParam("demoPathParam2", "demoPathParam2value");
		// Setting body
		reqBuilder.setBody(jsonString);
		
		// Getting RequestSpecification reference using builder() method
		RequestSpecification reqSpec = reqBuilder.build();
		
		
		RequestSpecification reqSpecHit = RestAssured.given(reqSpec);
		// Logging everything
		reqSpecHit.log().all();
		// Logging URI
		reqSpecHit.log().uri();
		// Logging request body
		reqSpecHit.log().body();
		// Logging request headers
		reqSpecHit.log().headers();
		// Logging request params
		reqSpecHit.log().params();
		// Logging request cookies
		reqSpecHit.log().cookies();
		  
		reqSpecHit.when()
		     .post().
		  then()
		     .assertThat()
		     .statusCode(500);			
				
	}
}

BDD Style program:-

package LoggingInRestAssured;

import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.specification.RequestSpecification;

public class LogAllExample {

	@Test
	public void logAll() {
		
		// There is no need to add escape character manually. Just paste string within double 
		// quotes. It will automatically add escape sequence as required. 
		String jsonString = "{\r\n" + 
				"    \"firstname\" : \"Amod\",\r\n" + 
				"    \"lastname\" : \"Mahajan\",\r\n" + 
				"    \"totalprice\" : 121,\r\n" + 
				"    \"depositpaid\" : true,\r\n" + 
				"    \"bookingdates\" : {\r\n" + 
				"        \"checkin\" : \"2020-02-01\",\r\n" + 
				"        \"checkout\" : \"2020-02-05\"\r\n" + 
				"    },\r\n" + 
				"    \"additionalneeds\" : \"Breakfast\"\r\n" + 
				"}";
		
		// Creating an object of RequestSpecBuilder
		RequestSpecBuilder reqBuilder = new RequestSpecBuilder();
		// Setting Base URI
		reqBuilder.setBaseUri("https://restful-booker.herokuapp.com");
		// Setting Base Path
		reqBuilder.setBasePath("/booking");
		// Setting demo headers
		reqBuilder.addHeader("DemoHeader1", "Headervalue1");
		reqBuilder.addHeader("DemoHeader2", "Headervalue2");
		// Setting demo cookies
		reqBuilder.addCookie("DemoCookie1", "DemoCookieValue1");
		reqBuilder.addCookie("DemoCookie2", "DemoCookieValue2");
		// Setting demo params
		reqBuilder.addParam("demoParam1", "demoParam1value");
		reqBuilder.addParam("demoParam2", "demoParam2value");
		// Setting demo path params
		reqBuilder.addPathParam("demoPathParam1", "demoPathParam1value");
		reqBuilder.addPathParam("demoPathParam2", "demoPathParam2value");
		// Setting body
		reqBuilder.setBody(jsonString);
		
		// Getting RequestSpecification reference using builder() method
		RequestSpecification reqSpec = reqBuilder.build();
		
		/*
		 * Since all logger method return RequestSpecification, we need to use log().. 
		 * We are passing unnecessary path params which will make test fail. But this is to learn logging request. 
		 * Ignore final API response. 
		 */
		RestAssured.
		  given(reqSpec)
		     .log().all() 
		     .log().uri()
		     .log().body()
		     .log().headers()
		     .log().params()
		     .log().cookies().
		  when()
		     .post().
		  then()
		     .assertThat()
		     .statusCode(500);			
				
	}
}

You will see below output in console:-

Request method:	POST
Request URI:	https://restful-booker.herokuapp.com/booking
Proxy:			
Request params:	demoParam1=demoParam1value
				demoParam2=demoParam2value
Query params:	
Form params:	
Path params:	demoPathParam1=demoPathParam1value
				demoPathParam2=demoPathParam2value
Headers:		DemoHeader1=Headervalue1
				DemoHeader2=Headervalue2
				Accept=*/*
				Content-Type=text/plain; charset=ISO-8859-1
Cookies:		DemoCookie1=DemoCookieValue1
				DemoCookie2=DemoCookieValue2
Multiparts:		
Body:
{
    "firstname" : "Amod",
    "lastname" : "Mahajan",
    "totalprice" : 121,
    "depositpaid" : true,
    "bookingdates" : {
        "checkin" : "2020-02-01",
        "checkout" : "2020-02-05"
    },
    "additionalneeds" : "Breakfast"
}
Request URI:	https://restful-booker.herokuapp.com/booking
Body:
{
    "firstname" : "Amod",
    "lastname" : "Mahajan",
    "totalprice" : 121,
    "depositpaid" : true,
    "bookingdates" : {
        "checkin" : "2020-02-01",
        "checkout" : "2020-02-05"
    },
    "additionalneeds" : "Breakfast"
}
Headers:		DemoHeader1=Headervalue1
				DemoHeader2=Headervalue2
				Accept=*/*
				Content-Type=text/plain; charset=ISO-8859-1
Request params:	demoParam1=demoParam1value
				demoParam2=demoParam2value
Query params:	
Form params:	
Path params:	demoPathParam1=demoPathParam1value
				demoPathParam2=demoPathParam2value
Multiparts:		
Cookies:		DemoCookie1=DemoCookieValue1
				DemoCookie2=DemoCookieValue2

You can clone/download example repo here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

2 thoughts on “REST Assured Tutorial 68 – Request Logging In Rest Assured”

Leave a Reply

Your email address will not be published. Required fields are marked *