In this tutorial, we will learn “How to pass/handle Path Parameters in Rest Assured using pathParam?” with example.
PathParam in RESTAssured...!!! Share on X
Here, we are going to cover the below topics:
Let’s begin:
1. What is Path Parameter?
‘Path Parameter’ is basically used to identify a specific resource or resources.
For example, In case, if we need to identify an employee based on his/her ‘id’, we are going to use ‘pathParam’.
GET /employee/{id}
Sometimes there is a confusion between URI Parameter (or Path Parameter) and Query Parameter. ‘Query Parameter’ is used to filter or sort the resources.
For example, In case, if we need to filter out the employees based on their class, we are going to use ‘queryParam’.
GET /employees?class=9
2. How to pass the Path Parameter (pathParam) in Rest Assured?
In this tutorial, we will test the ‘Dummy Sample Rest API’ which is available here. This page contains Fake Online REST API for the testing purpose which is performing various CRUD operations.
Let’s take an example of one of the API GET endpoints available at the above-mentioned website which is ‘/employee/{id}’ where the full-service URL with an endpoint is ‘http://dummy.restapiexample.com/api/v1/employee/{id}‘. It’s a simple GET request and we are trying to access the details about the employee whose id is 16914 which we are passing through ‘pathParam’ as part of the request in this below example:
Here is the code to send the request with Path Parameters to the above-mentioned Service Endpoint:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import static org.testng.Assert.assertEquals; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.response.Response; public class PathParamExample { @Test public void retrieveSpecificEmployee() { String id = "24947"; RestAssured.baseURI = "http://dummy.restapiexample.com/api/v1/employee/"; Response response = null; try { response = RestAssured.given() .pathParam("id", id) .when() .get("/{id}"); } catch (Exception e) { e.printStackTrace(); } System.out.println("Response :" + response.asString()); System.out.println("Status Code :" + response.getStatusCode()); System.out.println("Does Reponse contains 'employee_salary'? :" + response.asString().contains("employee_salary")); assertEquals(200, response.getStatusCode()); } } |
Eclipse Console Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [RemoteTestNG] detected TestNG version 6.14.3 Response :{"id":"24947","employee_name":"item66737","employee_salary":"123","employee_age":"23","profile_image":""} Status Code :200 Does Reponse contains 'employee_salary'? :true PASSED: retrieveSpecificEmployee =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 1, Failures: 0, Skips: 0 =============================================== |
That’s it, it’s that simple to handle Path Parameters in REST Assured API: ?
PathParam in RESTAssured...!!! Share on X
If you like this post, please check out my other useful blog posts on Rest Assured:
Other Useful References:
Getting the below exception
Invalid number of path parameters. Expected 0, was 1.