Path Parameter in Rest Assured

Path parameters are also part of the URL of a web service. Rest Assured similarly handles path parameters as that of the query parameter. pathParam() is the syntax used for the Path parameter. It is basically used to identify a specific resource or resources.

Let us assume the URL to make a GET Call to find pet details is  “https://petstore.swagger.io/v2/pet/1”. Thus, in this URL, the path parameter value is 1. Now let’s see how we can use rest assured to pass this path parameter.

 

How to pass the path parameters in GET request API Test using Rest Assured?

1. Set up a request with the specified base URI

 RestAssured.baseURI = "https://petstore.swagger.io/v2/pet";

Base URI is the root address of the Resource. Here we are specifying to REST assured to use the above as the base URI of the service.

 

2. Specify the exact resource by passing query parameter details

RestAssured.given()

.pathParam("petid",5)

.when()

.get("/{petid}")

Using pathParam() the value can be passed to the URL. In this case, the variable is defined as “petId” assigned with the value “5” which would be finally sent to URL in the form of a get request. Thus, the complete URL is specified using the Path Parameter as mentioned above.

Note: The same approaches can be used to handle multiple parameters in rest assured.

3. Response Validation

  .then().assertThat().statusCode(200);

Since it is a GET request for which we are making a call, thus the expected status code would be 200. Here, we are validating the response based on the condition where if status code from the response matches with the value 200.

Note:  Response validation of Response header and Response body can also be asserted, as discussed in previous examples. 

 

Complete Example Code

@Test

public void GetDetail() {


RestAssured.baseURI = "https://petstore.swagger.io/v2/pet";

RestAssured.given()

.pathParam("petid",5)

.when()

.get("/{petid}")


.then().assertThat().statusCode(200);

}