How to Handle HTTP Query Parameters Using REST Assured

When testing RESTful APIs, handling HTTP query parameters is a common requirement. Query parameters allow you to pass additional data in the URL, influencing the behavior of the API. REST Assured, a popular Java library for testing REST APIs, makes handling query parameters simple and efficient.

In this blog, we will explore how to use REST Assured to handle query parameters in various scenarios, with practical examples.


What Are Query Parameters?

Query parameters are appended to the URL after a ? and are typically used to filter or customize API responses. For example:

https://api.example.com/users?age=25&gender=male

Here, age and gender are query parameters, and their respective values are 25 and male.


Using Query Parameters in REST Assured

REST Assured provides methods to include query parameters when making requests. The two commonly used methods are:

  1. queryParam() – Adds a single query parameter.
  2. queryParams() – Adds multiple query parameters in one call.

Setting Up REST Assured

To start, add the REST Assured dependency to your pom.xml if you're using Maven:

<dependency>

    <groupId>io.rest-assured</groupId>

    <artifactId>rest-assured</artifactId>

    <version>5.3.0</version>

    <scope>test</scope>

</dependency>

1. Single Query Parameter

You can use the queryParam() method to pass a single query parameter to your API request.

import static io.restassured.RestAssured.*;

import io.restassured.response.Response;


public class QueryParamExample {

    public static void main(String[] args) {

        Response response = given()

            .baseUri("https://api.example.com")

            .queryParam("age", 25)  // Adding a single query parameter

        .when()

            .get("/users");

        

        response.prettyPrint();

    }

}

In this example, the request sent to the API would look like this:

GET https://api.example.com/users?age=25

2. Multiple Query Parameters

To add multiple query parameters, use the queryParams() method or chain multiple queryParam() calls.

Using queryParams():

import static io.restassured.RestAssured.*;

public class MultipleQueryParamsExample {
    public static void main(String[] args) {
        given()
            .baseUri("https://api.example.com")
            .queryParams("age", 25, "gender", "male") // Adding multiple query parameters
        .when()
            .get("/users")
        .then()
            .statusCode(200)
            .log().all();
    }
}

Chaining queryParam() Calls:

given()
    .baseUri("https://api.example.com")
    .queryParam("age", 25)
    .queryParam("gender", "male")
.when()
    .get("/users")
.then()
    .statusCode(200)
    .log().all();

3. Dynamic Query Parameters

You can handle dynamic query parameters by passing values at runtime, such as reading them from variables or external files.

String age = "30";

String gender = "female";


given()

    .baseUri("https://api.example.com")

    .queryParam("age", age)

    .queryParam("gender", gender)

.when()

    .get("/users")

.then()

    .statusCode(200)

    .log().all();

4. Query Parameters with Special Characters

When a query parameter value contains special characters (e.g., spaces, ampersands), REST Assured handles encoding for you. For example:

given()

    .baseUri("https://api.example.com")

    .queryParam("search", "SDET & QA") // Special character: '&'

.when()

    .get("/jobs")

.then()

    .statusCode(200)

    .log().all();

The request is automatically encoded as:

GET https://api.example.com/jobs?search=SDET%20%26%20QA

5. Combining Path and Query Parameters

REST Assured also allows combining path parameters and query parameters in the same request.

given()

    .baseUri("https://api.example.com")

    .pathParam("userId", 123)

    .queryParam("details", "full")

.when()

    .get("/users/{userId}")

.then()

    .statusCode(200)

    .log().all();

Request URL:

GET https://api.example.com/users/123?details=full

Validating Query Parameter Responses

You can validate query parameter responses using REST Assured’s assertion methods:

given()

    .baseUri("https://api.example.com")

    .queryParam("age", 25)

    .queryParam("gender", "male")

.when()

    .get("/users")

.then()

    .statusCode(200)

    .body("size()", greaterThan(0)) // Assert response has users

    .body("users[0].age", equalTo(25)) // Validate the first user's age

    .log().all();

Followers