Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 14 –RequestSpecification – How the request will look like

Posted on 03/21/2025 By admin

As a part of End to End REST Assured Tutorial, in this post, we will learn about RequestSpecification in Rest Assured. We will learn what, when, and how to use RequestSpecification in Rest Assured.

I have used below dependency of Rest Assured library for this post:-

  io.rest-assured rest-assured 4.3.1 test

Observe the below lines of codes carefully. You will find that we have some common statements in both @Test methods after given() method call.

package RequestSpecificationExamples;

import org.testng.annotations.Test;

import io.restassured.RestAssured;

public class WithoutUsingRequestSpecification {
        
        @Test
        public void getAllBookings()
        {
                // Given
                RestAssured
                  .given()
                  // Common baseURI and basePath
                         .baseUri("https://restful-booker.herokuapp.com")
                         .basePath("/booking")
                // When
                   .when()
                // Then
                   .then()
                   .statusLine("HTTP/1.1 200 OK");    
                
        }
        
        @Test
        public void getBookingDetailsWithInvalidFirstName()
        {
                // Given
                RestAssured
                  .given()
                  // Common baseURI and basePath
                         .baseUri("https://restful-booker.herokuapp.com")
                         .basePath("/booking")
                         .param("firstName", "Rahul")
                // When
                   .when()
                // Then
                   .then()
                   .statusLine("HTTP/1.1 200 OK");            
        }

}

Above we have only two tests but in real-time you may have many. A group of tests will have some common specifications to create a request. Following the DRY principle is the best way to write clean code. We have repeated common request specifications in both tests which is not a good practice. If we need to modify then we need to make changes every placed used.

To club common request specifications together and put as a common entity, we can use RequestSpecification in Rest Assured. RequestSpecification is an interface that allows you to specify how the request will look like. This interface has readymade methods to define base URL, base path, headers, etc. We need to use given() method of RestAssured class to get a reference for RequestSpecification. Remember RequestSpecification is an interface and we can not create an object of it. RequestSpecificationImpl is its implemented class.

A RequestSpecification with some specifications can be created as below:-

RequestSpecification requestSpecification = RestAssured.given();
requestSpecification.baseUri("https://restful-booker.herokuapp.com")
requestSpecification.basePath("/booking");

Or instead of calling RequestSpecification reference multiple times, we can use the builder pattern as below:-

RequestSpecification requestSpecification = 
        RestAssured.given()
                .baseUri("https://restful-booker.herokuapp.com")
                .basePath("/booking");

We can add a RequestSpecification to a request in multiple ways as shown below:-

RestAssured.given(requestSpecification)
OR
RestAssured.given().spec(requestSpecification)
package RequestSpecificationExamples;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

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

public class WithRequestSpecification {
        
        RequestSpecification requestSpecification;
        
        @BeforeClass
        public void setupRequestSpecification()
        {
                requestSpecification = RestAssured.given()
                .baseUri("https://restful-booker.herokuapp.com")
                .basePath("/booking");
        }
        
        @Test
        public void getAllBookings()
        {
                // Given
                RestAssured
                  .given()
                  .spec(requestSpecification)
                // When
                   .when()
                // Then
                   .then()
                   .statusLine("HTTP/1.1 200 OK");    
                
        }
        
        @Test
        public void getBookingDetailsWithInvalidFirstName()
        {
                // Given
                RestAssured
                  .given(requestSpecification)
                         .param("firstName", "Rahul")
                // When
                   .when()
                // Then
                   .then()
                   .statusLine("HTTP/1.1 200 OK");            
        }
}

There are two methods in RestAssured class to start creating Request specifications:-

The return type of both methods is RequestSpecification. There is no difference between the above two methods. The only difference is syntactical. An example code is below:-

package RequestSpecificationExamples;

import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

public class RequestSpecificationExample {

        public static void main(String[] args) {

                // Creating request specification using given()
                RequestSpecification request1 = RestAssured.given();
                // Setting Base URI
                request1.baseUri("https://restful-booker.herokuapp.com");
                // Setting Base Path
                request1.basePath("/booking");

                // Creating request specification using with()
                RequestSpecification request2 = RestAssured.with();
                // Setting Base URI
                request2.baseUri("https://restful-booker.herokuapp.com");
                // Setting Base Path
                request2.basePath("/ping");

                // You can also use builder pattern as below
                RequestSpecification request3 = RestAssured.with();
                request3.baseUri("https://restful-booker.herokuapp.com").basePath("/ping");

        }

}

You can download/clone the above sample project from here.

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

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: How to launch Chrome browser as headless in Selenium Java
Next Post: Part 12: Usages Of Javascripts In Selenium : How To Type In Input Box Using Javascript

Related Posts

TestNG Tutorials 50: DataProvider in TestNG – Understand Basics of DataProvider & How It Works Uncategorized
June 9, 2017 – Make Selenium Easy Uncategorized
March 2, 2019 – Make Selenium Easy Uncategorized
September 29, 2019 – Make Selenium Easy Uncategorized
Java Interview Question – Explain System.out.println(arg) Uncategorized
Why And How Should We Terminate Browser Driver Executable File In Selenium WebDriver 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