Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 68 – Request Logging In Rest Assured

Posted on 02/19/2025 By admin

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.

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);                  
                                
        }
}

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

Uncategorized

Post navigation

Previous Post: TestNG Tutorials 61: Dependency in TestNG – Creating Dependency Among Test Methods – DependsOnGroup
Next Post: Part 10: Usages Of Javascripts In Selenium: How To Capture Screenshot Of Entire Web Page

Related Posts

Why And How Should We Terminate Browser Driver Executable File In Selenium WebDriver Uncategorized
allPkgFromMain – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
Frequently Asked Java Program 20: Java Program to Find Missing Alphabets in Given String Uncategorized
image – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark