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
- Start from
given()and attach one parameter namedquerywith the value John usingqueryParam(). - Send a GET to
/searchand keep theResponse. - Read the match count from
meta.totalwithgetInt()and print it afterTotal:. - Read the first result's name and category from
data[0]withgetString()and print them afterName:andCategory:.
Common mistakes
Guessing the parameter name
The server only understands
query. A parameter calledqornameis 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,
datais an empty array anddata[0].namecomes back asnullinstead of throwing. Checkmeta.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().
Related lessons
- Filter by CategoryUses the same /search endpoint with a category filter instead of free text.
- PaginationReads the rest of the meta object to work with pages.
- Extract Nested ValuesPractises nested dot paths like the meta.total read here.
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.*;
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
}
}