Skip to content
CodeKruPro

Simple Text Search

35/62

Simple Text Search

Use .queryParam() to add URL parameters:

given().queryParam("name", "value").get(path);  // requests path?name=value

The /search endpoint filters across users, products, and books. The query text matches an item's name or description, ignoring case. It returns {"data": [...], "meta": {"total": N, ...}}.

Task: Search for "John" and print the result.

Expected output:

Total: 1
Name: John Doe
Category: users

About this lesson

REST Assured queryParam: send a search term in the URL of a GET request

Learn REST AssuredIntermediateQuery Parameters and Search

How it works

A query parameter is a name and value appended after the ? of a URL. Search, filter and paging options almost always travel this way, because a GET request has no body to carry them. In REST Assured you never glue that string together yourself: queryParam(name, value) on the request specification adds one pair, and the library builds and encodes the final URL when the request is sent. The value parameter is an Object, so a number works as well as a string.

The testkru /search endpoint looks through fifteen records (five users, five products, five books). The query parameter keeps a record when its name or its description contains the text, and the comparison ignores case, so JOHN and john find the same single match. The answer is an object with two parts: data, an array of the matching records (each with id, name, category and description), and meta, which carries the counts.

Because the payload is nested, read it with dotted and indexed paths. meta.total reaches into the meta object, and data[0].name picks the first element of the array and then a field inside it. getInt() hands back a Java int, while getString() gives a String.

About the assignment

Search the shared dataset for the word John and report how many records matched, plus the name and category of the first hit.

What you will do

  1. Start from given() and attach one parameter named query with the value John using queryParam().
  2. Send a GET to /search and keep the Response.
  3. Read the match count from meta.total with getInt() and print it after Total:.
  4. Read the first result's name and category from data[0] with getString() and print them after Name: and Category:.

Common mistakes

  • Guessing the parameter name

    The server only understands query. A parameter called q or name is silently ignored, so the response is the unfiltered first page with a total of fifteen. Read the API documentation for the exact spelling instead of trusting a convention from another service.

  • Writing the question mark into the path

    Hard-coding the search string after the path works for one simple word, but it mixes data into the route and gets messy once values contain spaces or several options must be combined. queryParam() keeps each option separate and readable.

  • Reading the first element of an empty result

    If nothing matches, data is an empty array and data[0].name comes back as null instead of throwing. Check meta.total (or the size of the list) before trusting the first element, otherwise a test can print null and still look green.

In a real test suite

Search tests usually run a table of inputs through the same request: an exact term, a term in a different case, a fragment that should match the description, and a term that should find nothing. With JUnit 5 that becomes a parameterised test where each row asserts meta.total with equalTo() and the first name with then().body().

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.

Main.java
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.*;

public class Main {
    public static void main(String[] args) {
        RestAssured.baseURI = "https://testkru.com/api";

        // TODO: GET /search with queryParam "query" = "John"
        // TODO: Print meta.total, data[0].name, and data[0].category
    }
}