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:-

BDD Style program:-

You will see below output in console:-

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.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply