REST Assured Tutorial 16 – Building RequestSpecification Using RequestSpecBuilder


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.

Prerequisite post

RequestSpecification – How The Request Will Look Like

Multiple Ways Of Calling HTTP Methods On A RequestSpecification

Class RequestSpecBuilder

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.

Example Program

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




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.

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

9 thoughts on “REST Assured Tutorial 16 – Building RequestSpecification Using RequestSpecBuilder

  1. Seems that there is one small issue on line number 23. I believe it should be something like this->Response res1 = RestAssured.given(reqSpec).get();
    ..Thank you so much Amod Mahajan for these tutorials.

  2. is there any time we will be using this because we can directly use RequestSpecifiation as its increasing syntax not helping anyways?

    Any use case for this?

    Will that help separating out the request related data..
    so we can have multiple things like this

    RequestSpecification req1 =
    RequestSpecification req2 =
    RequestSpecification req3 =
    RequestSpecification req4 =

    let me know if any way this will be useful

    1. Hi Gaurav,
      is this code working for you.When I am trying to use RequestSpecBuilder, I am getting below error:-

      java.lang.NullPointerException: Cannot get property ‘assertionClosure’ on null object

      Any idea regarding this?

    2. ResponseSpecBuilder and RequestSpecBuilder are just another way of creating ResponseSpecification and RequestSepcification respectively. ResponseSpecBuilder and RequestSpecBuilder contain more meaningful methods by name. Yes as above you can have multiple specifications and reuse them. You can also put common specifications together and use them in your request. You may have multiple specs in a request.

  3. I have referred lots of sites, bolgs and you-tube video’s but I always gets confused. Thank you so much Amod for this systematic and very explanative tutorials, now I’m getting confidence in APi testing using Rest assured. Keep it up…….

Leave a Reply

Your email address will not be published. Required fields are marked *