Beginner Chapter 5 · 8 min read

Query Parameters & Path Params

Use query strings for filtering, sorting, and pagination. Understand path parameters for resource identification.

Query Parameters & Path Parameters

APIs use two types of URL parameters to locate and filter resources. Path parameters identify a specific resource (e.g., /users/42), while query parameters filter, sort, or paginate collections (e.g., /users?role=admin&sort=name).

Path Parameters

Path params are part of the URL path itself. They typically identify a single resource: /posts/{id}, /users/{userId}/comments. The server extracts the value to locate the exact resource.

Query Parameters

Query params come after the ? in the URL and are separated by &. They're used for filtering results (?status=active), pagination (?page=2&limit=10), sorting (?sort=created&order=desc), and searching (?q=keyword).

Testing Considerations

Always test with valid params, invalid params, missing required params, special characters, and boundary values. Verify that the API correctly filters the response based on the parameters you send.

query-params.test.js
// Path parameter — get specific resource
const userId = 1;
const response = await fetch(
  `https://jsonplaceholder.typicode.com/users/${userId}`
);
const user = await response.json();
console.assert(user.id === userId, 'Should return correct user');

// Query parameters — filter posts by userId
const params = new URLSearchParams({ userId: '1' });
const filtered = await fetch(
  `https://jsonplaceholder.typicode.com/posts?${params}`
);
const posts = await filtered.json();
console.assert(posts.every(p => p.userId === 1), 'All posts should be from user 1');
console.log(`User 1 has ${posts.length} posts`);

// Multiple query parameters
const multiParams = new URLSearchParams({
  _page: '1',
  _limit: '5'
});
const paginated = await fetch(
  `https://jsonplaceholder.typicode.com/posts?${multiParams}`
);
const page = await paginated.json();
console.assert(page.length <= 5, 'Should return at most 5 items');

// Nested resource (path params for relationship)
const commentsResp = await fetch(
  `https://jsonplaceholder.typicode.com/posts/${1}/comments`
);
const comments = await commentsResp.json();
console.assert(comments.length > 0, 'Post should have comments');
console.assert(comments.every(c => c.postId === 1));
console.log('All parameter tests passed!');
QueryParamsTest.java
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
import org.testng.annotations.Test;

public class QueryParamsTest {

    private static final String BASE_URI = "https://jsonplaceholder.typicode.com";

    @Test
    public void testPathParameter() {
        given()
            .baseUri(BASE_URI)
            .pathParam("id", 1)
        .when()
            .get("/users/{id}")
        .then()
            .statusCode(200)
            .body("id", equalTo(1))
            .body("name", notNullValue());
    }

    @Test
    public void testQueryParameter_FilterByUserId() {
        given()
            .baseUri(BASE_URI)
            .queryParam("userId", 1)
        .when()
            .get("/posts")
        .then()
            .statusCode(200)
            .body("size()", greaterThan(0))
            .body("userId", everyItem(equalTo(1)));
    }

    @Test
    public void testMultipleQueryParams_Pagination() {
        given()
            .baseUri(BASE_URI)
            .queryParam("_page", 1)
            .queryParam("_limit", 5)
        .when()
            .get("/posts")
        .then()
            .statusCode(200)
            .body("size()", lessThanOrEqualTo(5));
    }

    @Test
    public void testNestedResourcePath() {
        given()
            .baseUri(BASE_URI)
            .pathParam("postId", 1)
        .when()
            .get("/posts/{postId}/comments")
        .then()
            .statusCode(200)
            .body("size()", greaterThan(0))
            .body("postId", everyItem(equalTo(1)));
    }
}
test_query_params.py
import requests

BASE_URL = 'https://jsonplaceholder.typicode.com'

def test_path_parameter():
    """Path param to get specific resource"""
    user_id = 1
    response = requests.get(f'{BASE_URL}/users/{user_id}')

    assert response.status_code == 200
    assert response.json()['id'] == user_id

def test_query_param_filter():
    """Query param to filter posts by userId"""
    response = requests.get(
        f'{BASE_URL}/posts',
        params={'userId': 1}
    )

    posts = response.json()
    assert len(posts) > 0
    assert all(p['userId'] == 1 for p in posts), 'All posts should be from user 1'
    print(f"User 1 has {len(posts)} posts")

def test_multiple_query_params():
    """Multiple query params for pagination"""
    response = requests.get(
        f'{BASE_URL}/posts',
        params={'_page': 1, '_limit': 5}
    )

    posts = response.json()
    assert len(posts) <= 5, 'Should return at most 5 items'

def test_nested_resource():
    """Path params for nested resources"""
    post_id = 1
    response = requests.get(f'{BASE_URL}/posts/{post_id}/comments')

    comments = response.json()
    assert len(comments) > 0, 'Post should have comments'
    assert all(c['postId'] == post_id for c in comments)

API Testing Beginner Query Parameters & Path Params

Written by PV

© 2026 All Rights Reserved