About this lesson
REST Assured: filter API results by category with a query parameter
Learn REST AssuredIntermediateQuery Parameters and Search
How it works
Filtering differs from searching in one important way: a filter is an exact match on a known field, not a fuzzy text lookup. On testkru /search the category parameter keeps only records whose category equals the value, and the allowed values are users, products and books. The server lowercases the parameter before comparing, so Books behaves like books, but a near miss such as book matches nothing and returns a total of zero with an empty data array.
To collect one field from every element, give JsonPath the array name followed by the field. getList("data.name") walks the array and returns a List of the names in response order. Printing a Java list uses its toString(), which wraps the items in square brackets and separates them with a comma and a space.
Results come back ordered by name using a plain byte comparison, so a title that starts with a digit sorts before any letter. Keep in mind that data only holds one page: the default page size is five. The meta.total count, on the other hand, covers every match, so the two numbers agree here only because exactly five books exist.
About the assignment
Narrow the search results to books only, then report how many there are and list every title in the order the API returns them.
What you will do
- Add a
categoryparameter with the value books to the request usingqueryParam(). - Fire the GET at
/search, holding on to what comes back. - Print the total from
meta.totalafterTotal:. - Collect every
nameinsidedatainto aList<String>withgetList(). - Print that list after the label
Books:so the bracketed form appears as is.
Common mistakes
Using the singular category name
The values are plural.
bookorproductgive zero results without any error status, so a test that only checks for 200 would pass on an empty list. Assert on the total or onhasSize()as well.Assuming data holds every match
With the default page size of five, a category with more records would be cut off while
meta.totalstill showed the full count. When you need everything, raiselimitor walk the pages, and never compare the list size to the total without thinking about paging.Sorting the list again in Java
Calling
Collections.sort()on the names hides whether the server sorted them. If ordering is part of the contract, assert the list as received, for example withcontains()from Hamcrest, which checks order too.
In a real test suite
Category filters are a classic place for regression bugs: a new value is added, or matching becomes case sensitive after a refactor. A good suite checks every allowed value, one invalid value and the mixed-case form, and asserts both the count and that each returned record really carries the requested category, for instance with everyItem(equalTo()) on data.category.
Related lessons
- Simple Text SearchQueries the same endpoint with a free-text search term.
- Extract List of ValuesIntroduces collecting one field across an array with getList().
- Products by CategoryFilters products by category through a path parameter instead of a query parameter.
To try one of these calls on any API endpoint without a task, open the Online Selenium Java Compiler (REST Assured is included).
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.*;
import java.util.List;
public class Main {
public static void main(String[] args) {
RestAssured.baseURI = "https://testkru.com/api";
// TODO: GET /search with queryParam "category" = "books"
// TODO: Print the total and a list of all book names
}
}