Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 18 – Querying RequestSpecification 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 about “Querying RequestSpecification in Rest Assured”.

We know that RequestSpecification is used to create a request or how the request will look like. We set base URI, base path, headers, etc to request using RequestSpecification. There is a way available to query this RequestSpecification as well so that at any point of time we can retrieve details of RequestSpecification like what is base URI is set etc.

To query or retrieve details from RequestSpecification, Rest Assured provides a class named SpecificationQuerier. This class has a static method called query(RequestSpecification specification) which returns reference of QueryableRequestSpecification interface. This interface has methods to retrieve request details such as getBasePath(), getBody() etc.

package RestAssuredConcepts;

import io.restassured.RestAssured;
import io.restassured.http.Header;
import io.restassured.http.Headers;
import io.restassured.specification.QueryableRequestSpecification;
import io.restassured.specification.RequestSpecification;
import io.restassured.specification.SpecificationQuerier;

public class QueryingRequestSpecificationExample {
        
        public static void main(String[] args) {
                
                // I am adding dummy request details for example
                String JsonBody = "{\"firstName\":\"Amod\"}";
                
                // Creating request specification using given()
                RequestSpecification request1= RestAssured.given();
                // Setting Base URI
                request1.baseUri("https://restful-booker.herokuapp.com")
                // Setting Base Path
                .basePath("/booking")
                .body(JsonBody)
                .header("header1", "headerValue1")
                .header("header2", "headerValue2");
                
                // Querying RequestSpecification
                // Use query() method of SpecificationQuerier class to query 
                QueryableRequestSpecification queryRequest = SpecificationQuerier.query(request1);
                
                // get base URI
                String retrieveURI = queryRequest.getBaseUri();
                System.out.println("Base URI is : "+retrieveURI);
                
                // get base Path
                String retrievePath = queryRequest.getBasePath();
                System.out.println("Base PATH is : "+retrievePath);
                
                // get Body
                String retrieveBody = queryRequest.getBody();
                System.out.println("Body is : "+retrieveBody);
                
                // Get Headers
                Headers allHeaders = queryRequest.getHeaders();
                System.out.println("Printing all headers: ");
                for(Header h : allHeaders)
                {
                        System.out.println("Header name : "+ h.getName()+" Header value is : "+h.getValue());
                }
        }

}

Output:-

Base URI is : https://restful-booker.herokuapp.com
Base PATH is : /booking
Body is : {"firstName":"Amod"}
Printing all headers: 
Header name : header1 Header value is : headerValue1
Header name : header2 Header value is : headerValue2

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: JsonPath – RestAssured
Next Post: API Testing – REST Assured – Page 6

Related Posts

Git Tutorial 14 – How To Create, Switch And List Branches In Git? Uncategorized
December 2018 – Make Selenium Easy Uncategorized
Selenium Topics Archives – Page 3 of 37 – Make Selenium Easy Uncategorized
October 4, 2018 – Make Selenium Easy Uncategorized
TestNG Tutorials 46: Overriding Parameters in TestNG | Make Selenium Easy Uncategorized
tooltip – 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