Request Specifications
As your API tests become more complex, you will need to customize your requests with headers, query parameters, cookies, and more. REST Assured provides a powerful feature called Request Specification to make your tests cleaner and easier to maintain.
What is a Request Specification?
A Request Specification in REST Assured allows you to define reusable settings for API requests. This helps you avoid writing the same code repeatedly for every test.
With Request Specification, you can:
- Set base URIs and endpoints.
- Add headers, query parameters, and cookies.
- Reuse these settings across multiple tests.
Why Use Request Specifications?
- Avoid Code Duplication – Define common settings once.
- Improve Readability – Keep your tests clean and simple.
- Increase Flexibility – Easily update settings in one place.
Example Scenario
Let's say you want to test an API that requires:
- Base URL: http://1xx.xxx.xx.xxx:8080
- Content Type: application/json
Create a Request Specification
Here's how to define a Request Specification:
import io.restassured.RestAssured; import io.restassured.specification.RequestSpecification; import io.restassured.response.Response; import static io.restassured.RestAssured.*; import static org.hamcrest.Matchers.*; public class AdvancedRequestSpec { public static void main(String[] args) { // Create a reusable Request Specification RequestSpecification requestSpec = given() .baseUri("http://1xx.xxx.xx.xxx:8080") .contentType("application/json"); // Use the Request Specification to send a GET request Response response = given() .spec(requestSpec) // Reuse the specification .when() .get("/books/20250401"); // Validate and print the response response.then().statusCode(200); System.out.println("Response: " + response.getBody().asString()); } }
Understanding the Code
Let's break down the important parts:
1. Create the Request Specification
RequestSpecification requestSpec = given() .baseUri("http://1xx.xxx.xx.xxx:8080") .contentType("application/json");
- baseUri() – Defines the API's base URL.
- contentType() – Specifies the content format (JSON).
2. Reuse the Specification
given().spec(requestSpec).when().get("/books/20250401");
- spec() – Applies the reusable request configuration.
Adding Query Parameters
Use query parameters to filter results or customize the request.
Example:
Fetch Books with the book name Book1:
RequestSpecification requestSpec = given() .baseUri("http://1xx.xxx.xx.xxx:8080") .queryParam("name", "Book 1"); Response response = given() .spec(requestSpec) .when() .get("/books"); response.then().statusCode(200); System.out.println("Filtered Response: " + response.getBody().asString());
Adding Path Parameters
Use path parameters to specify dynamic parts of a URL.
Example:
Get details for book with ISBN 20250401:
RequestSpecification requestSpec = given() .baseUri("http://1xx.xxx.xx.xxx:8080"); Response response = given() .spec(requestSpec) .pathParam("isbn", 20250401) // Set the path parameter .when() .get("/books/{isbn}"); response.then().statusCode(200); System.out.println("Book Details: " + response.getBody().asString());
Adding Cookies
Some APIs require cookies for authentication or tracking.
Example
RequestSpecification requestSpec = given() .baseUri("https://example.com") .cookie("sessionId", "abc123"); Response response = given() .spec(requestSpec) .when() .get("/profile"); response.then().statusCode(200); System.out.println("Cookie Response: " + response.getBody().asString());
Logging Requests and Responses
Enable logging to debug and view API request and response details.
Example:
RequestSpecification requestSpec = given() .baseUri("http://1xx.xxx.xx.xxx:8080") .log().all(); // Log request details Response response = given() .spec(requestSpec) .when() .get("/books/20250401"); response.then().log().all(); // Log response details
Reusing Request Specifications in Multiple Tests
You can extract your Request Specification into a reusable method.
Example:
public class ApiUtils { public static RequestSpecification getRequestSpec() { return given() .baseUri("http://1xx.xxx.xx.xxx:8080") .contentType("application/json"); } } public class BooksTests { public static void main(String[] args) { // Use the reusable Request Specification Response response = given() .spec(ApiUtils.getRequestSpec()) .when() .get("/books/20250401"); response.then().statusCode(200); System.out.println("Book Data: " + response.getBody().asString()); } }
Combining Multiple Specifications
You can merge different parts of the request using .spec() multiple times.
Example:
RequestSpecification baseSpec = given() .baseUri("http://1xx.xxx.xx.xxx:8080"); RequestSpecification authSpec = given() .header("Authorization", "Bearer token123"); Response response = given() .spec(baseSpec) .spec(authSpec) .when() .get("/books/20250401"); response.then().statusCode(200);