REST Assured Tutorial 15 – Multiple Ways of Calling HTTP Methods on a RequestSpecification

As a part of End to End REST Assured Tutorial , in this post We will learn about calling HTTP methods ( get, post, put etc) on a RequestSpecification.

We have already seen what is Request Specification in Rest Assured. The major confusing and interesting thing about Rest Assured is that you have multiple ways of performing the same action which makes beginner confused. Rest Assured is full of syntactic sugar methods.

See the example program below to understand different ways of calling http methods on RequestSpecification :-

package RestAssuredConcepts;

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

public class DifferentWaysOfCallingHttpMethodsOnRequestSpecifications {
	
	
	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");
		
		
		// We can directly call http verbs on RequestSpecification
		Response res1= request1.get();
		System.out.println(res1.asString());
		
		// We can also pass RequestSpecification reference variable in overloaded given() method
		Response res2 = RestAssured.given(request1).get();
		System.out.println(res2.asString());
		
		// We can also pass RequestSpecification using spec() method
		Response res3 = RestAssured.given().spec(request1).get();
		System.out.println(res3.asString());
		
	
	}

}

Output:-

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

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.

1 thought on “REST Assured Tutorial 15 – Multiple Ways of Calling HTTP Methods on a RequestSpecification”

Leave a Reply

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