GET is the most common HTTP method. You will write more GET tests than anything else. Let us cover every variation — query params, path params, headers, and extracting different parts of the response.
Query params go after the ? in a URL. Like /posts?userId=1. In REST Assured, you add them in given().
@Test
public void testGetPostsByUser() {
given()
.queryParam("userId", 1) // Adds ?userId=1 to the URL
.when()
.get("/posts")
.then()
.statusCode(200)
.body("size()", equalTo(10)) // User 1 has 10 posts
.body("userId", everyItem(equalTo(1))); // Every post belongs to user 1
}
@Test
public void testMultipleQueryParams() {
given()
.queryParam("_page", 1) // First page
.queryParam("_limit", 5) // 5 items per page
.when()
.get("/posts")
.then()
.statusCode(200)
.body("size()", equalTo(5)); // Only 5 posts returned
}Path params are part of the URL itself. Like /posts/1 where 1 is the post ID. You define placeholders in curly braces and fill them with pathParam().
@Test
public void testPathParam() {
given()
.pathParam("id", 42)
.when()
.get("/posts/{id}")
.then()
.statusCode(200)
.body("id", equalTo(42));
}
// Multiple path params
@Test
public void testNestedResource() {
given()
.pathParam("postId", 1)
.when()
.get("/posts/{postId}/comments")
.then()
.statusCode(200)
.body("size()", equalTo(5))
.body("postId", everyItem(equalTo(1))); // All comments belong to post 1
}@Test
public void testWithHeaders() {
given()
.header("Accept", "application/json")
.header("Authorization", "Bearer my-token-123")
.header("X-Custom-Header", "TesterRank")
.when()
.get("/posts/1")
.then()
.statusCode(200);
}
// Using contentType shortcut
@Test
public void testWithContentType() {
given()
.contentType(ContentType.JSON) // Same as header("Content-Type", "application/json")
.accept(ContentType.JSON) // Same as header("Accept", "application/json")
.when()
.get("/posts")
.then()
.statusCode(200);
}import io.restassured.response.Response;
@Test
public void testExtractResponseParts() {
Response response =
given()
.when()
.get("/posts/1")
.then()
.statusCode(200)
.extract()
.response();
// Status code
int statusCode = response.statusCode();
System.out.println("Status: " + statusCode); // 200
// Response body as String
String body = response.body().asString();
System.out.println("Body: " + body);
// Specific header
String contentType = response.header("Content-Type");
System.out.println("Content-Type: " + contentType);
// Response time in ms
long responseTime = response.time();
System.out.println("Response time: " + responseTime + "ms");
}@Test
public void testResponseHeaders() {
given()
.when()
.get("/posts/1")
.then()
.statusCode(200)
.header("Content-Type", containsString("application/json"))
.header("Cache-Control", notNullValue())
.time(lessThan(3000L)); // Response must come within 3 seconds
}The .time(lessThan(3000L)) assertion is gold for performance checks. Add it to critical endpoints. If the API suddenly becomes slow, your test catches it. The L suffix makes it a long — REST Assured needs a long, not an int.
queryParam adds ?key=value. pathParam fills {key} in the URL. Do not mix them up. If your URL is /users/{id}?status=active, use pathParam for id and queryParam for status.
Q: How do you pass query parameters and path parameters in REST Assured?
A: Query parameters use given().queryParam("key", "value") — they get appended to the URL as ?key=value. Path parameters use given().pathParam("id", 1) with a placeholder in the URL like get("/users/{id}"). For multiple query params, chain multiple queryParam() calls. You can also pass a Map to queryParams(). The key difference: query params are for filtering/pagination, path params are for identifying a specific resource.
Key Point: queryParam() adds ?key=value. pathParam() fills {placeholder} in URLs. Use header() for custom headers and contentType() as a shortcut. Extract response parts with .extract().response().