Make Selenium Easy

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

Last updated on March 27th, 2020 at 01:32 am

As a part of End to End REST Assured Tutorial , in this post We will learn about an important interface named “RequestSpecification” in Rest Assured.

RequestSpecification is an Interface whose implemented class is
RequestSpecificationImpl. This interface helps you to create request or how the request will look like.

For example:-

  • What will be the base uri of request?
  • What will be the base path of request?
  • Which type of authentication is required?
  • How the request body will be passed?
  • How to define content type?
  • How to add cookies, headers , proxy , path parameters , query parameters etc.

Now the question is why should we use RequestSpecification because whatever points we saw above, all can be done using direct methods.

In fact, RequestSpecification and ResponseSpecification both are for Re-use specifications. Suppose, you need to write 10 API tests and all APIs have common BaseURI, BasePath, some headers, authentication, cookies etc. You have a way to define these repetitive information for each tests but maintenance will be difficult if something new to added or remove. Here, RequestSpecification or ResponseSpecification solves your problem. It allow s you to define at one place so that you can easily add/manage request specifications.

Without RequestSpecifications:

You can see there are two common things in above tests:- BaseURI and BasePath and I have used duplicate lines in both. There may be many common specifications among tests. It is good practice to keep common specification at common place i.e. RequestSpecification.

With RequestSpecification:-

In short, RequestSpecification interface contains direct methods to perform above operations and there are many more. You call find all methods of RequestSpecification here. We have already used RequestSpecification interface to construct request in previous posts. Return type of many methods is RequestSpecification which helps you to write script using builder pattern.

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

  1. given()
  2. with()

Return type of both methods is RequestSpecification. There are no difference between above two methods. Only difference is syntactical. Example code is below:-

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.

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.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

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

by Amod Mahajan time to read: 3 min
%d bloggers like this: