Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 16 – Building RequestSpecification Using RequestSpecBuilder

Posted on 04/23/2025 By admin

As a part of the End to End REST Assured Tutorial, in this post, we will learn about class RequestSpecBuilder and how it can be used to create RequestSpecification in Rest Assured.

RequestSpecification – How The Request Will Look Like

Multiple Ways Of Calling HTTP Methods On A RequestSpecification

There is another way of creating RequestSpecification in Rest Assured and that is by using class RequestSpecBuilder.

RequestSpecBuilder is a class in Rest Assured, which contains methods to set cookies, headers, multipart details, body, authentication, form parameters, query parameters, path parameters, base path, base URI, proxy, etc. These all are required to construct a Requestspecification. After adding all required details, we need to use “build()” method of RequestSpecBuilder class to get a RequestSpecification reference.

RequestSpecification helps us to reuse specifications.

package RestAssuredConcepts;

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

public class RequestSpecBuilderExample {

        public static void main(String[] args) {

                // 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");
                // Getting RequestSpecification reference using builder() method
                RequestSpecification reqSpec = reqBuilder.build();
                
                // Usage in different styles
                // We can directly call http verbs on RequestSpecification
                Response res1= reqSpec.get();
                System.out.println(res1.asString());
                System.out.println("======================");
                
                // We can also pass RequestSpecification reference variable in overloaded given() method
                Response res2 = RestAssured.given(reqSpec).get();
                System.out.println(res2.asString());
                System.out.println("======================");
                                
                // We can also pass RequestSpecification using spec() method
                Response res3 = RestAssured.given().spec(reqSpec).get();
                System.out.println(res3.asString());

        }
}
[{"bookingid":11},{"bookingid":13},{"bookingid":15},{"bookingid":1},{"bookingid":6},{"bookingid":2},{"bookingid":8},{"bookingid":4},{"bookingid":5},{"bookingid":3},{"bookingid":10},{"bookingid":12},{"bookingid":7},{"bookingid":14},{"bookingid":9}]
======================
[{"bookingid":11},{"bookingid":13},{"bookingid":15},{"bookingid":1},{"bookingid":6},{"bookingid":2},{"bookingid":8},{"bookingid":4},{"bookingid":5},{"bookingid":3},{"bookingid":10},{"bookingid":12},{"bookingid":7},{"bookingid":14},{"bookingid":9}]
======================
[{"bookingid":11},{"bookingid":13},{"bookingid":15},{"bookingid":1},{"bookingid":6},{"bookingid":2},{"bookingid":8},{"bookingid":4},{"bookingid":5},{"bookingid":3},{"bookingid":10},{"bookingid":12},{"bookingid":7},{"bookingid":14},{"bookingid":9}]

Note:- You might get an exception “java.lang.NullPointerException: Cannot get property ‘assertionClosure’ on null object” on running above program. There is an existing bug in Rest Assured which is in an open state as of now. You can comment line no 23 and 24 “Response res1= reqSpec.get();” to run program successfully.

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: Git Tutorial 26 – How To Ignore Already Indexed/staged/Committed/Pushed File to Git Repo
Next Post: Upload multiple files in Selenium

Related Posts

amod mahajan Uncategorized
REST Assured Tutorial 14 –RequestSpecification – How the request will look like Uncategorized
Understand Defect Life Cycle In Testing Uncategorized
htmlinforefox – Make Selenium Easy Uncategorized
refresh token Uncategorized
Learn Selenium With Quiz – Basic Level 4 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