← Back to libraryQuestion 234 of 468
🔌REST AssuredBeginner

Path and Query Parameters

📌 Definition:

REST Assured adds URL parameters via .pathParam()/path templates for path variables and .queryParam() for query-string values, keeping URLs clean and reusable.

📖 Detailed Explanation:

Path parameters fill placeholders in the endpoint: .pathParam('id', 5).get('/users/{id}') or inline .get('/users/{id}', 5), producing /users/5. Query parameters append to the query string: .queryParam('page', 2).queryParam('size', 20).get('/users') produces /users?page=2&size=20. There are also .formParam() for form-encoded bodies and .queryParams(Map) for many at once. Using parameter methods (rather than string-concatenating the URL) handles encoding correctly and keeps tests readable and data-driven — you can parameterize the values from a TestNG DataProvider.

🔑 Key Points:
  • pathParam / {template} fills path variables
  • queryParam adds query-string values (?page=2)
  • formParam for form-encoded bodies; queryParams(Map) for many
  • Prefer param methods over string concatenation (encoding)
🌍 Real-World Example:

A search test: given().queryParam('q', 'login test').queryParam('limit', 5).when().get('/search') — REST Assured URL-encodes the space in 'login test' automatically.

🎯 Scenario-Based Interview Question:

A tester builds the URL as '/search?q=' + userInput and gets failures when the input has spaces or &. What's the fix?