Types of Parameters in REST Assured - Notes By ShariqSP

Types of Parameters in REST Assured

When working with REST Assured, parameters are used to send data to the server as part of the HTTP request. These parameters help in defining the request context and passing data to the API. There are four main types of parameters in REST Assured:

1. Query Parameters

Query parameters are appended to the URL after a question mark (`?`). They are used to filter or sort the response or provide additional information for the API request. Each query parameter is represented as a key-value pair, and multiple parameters can be concatenated using the ampersand (`&`).

                    
                        given()
                            .queryParam("key", "value")
                            .queryParam("filter", "active")
                        .when()
                            .get("/api/resource")
                        .then()
                            .statusCode(200);
                    
                

In the above example, the query parameters `key=value` and `filter=active` are appended to the URL.

2. Path Parameters

Path parameters are part of the URL itself and are used to specify a specific resource. These are typically enclosed in curly braces (`{}`) in the URL template and replaced with actual values at runtime.

                    
                        given()
                            .pathParam("id", 123)
                        .when()
                            .get("/api/resource/{id}")
                        .then()
                            .statusCode(200);
                    
                

In this example, the `id` placeholder in the URL template is replaced with `123`, making the final URL `/api/resource/123`.

3. Form Parameters

Form parameters are typically used in POST requests to send data as part of the request body. They are often used for submitting forms or sending structured data to the API.

                    
                        given()
                            .formParam("username", "john_doe")
                            .formParam("password", "123456")
                        .when()
                            .post("/login")
                        .then()
                            .statusCode(200);
                    
                

Here, `username` and `password` are form parameters sent in the request body.

4. Generic Parameters Using param

The `param` method in REST Assured is a versatile option for adding parameters. It can be used for both query and form parameters. REST Assured will determine whether the parameter should be added to the URL (for GET requests) or the body (for POST requests), based on the HTTP method used.

                    
                        // Using param for query parameters
                        given()
                            .param("search", "test")
                            .param("page", 2)
                        .when()
                            .get("/api/resource")
                        .then()
                            .statusCode(200);
            
                        // Using param for form parameters
                        given()
                            .param("username", "john_doe")
                            .param("password", "123456")
                        .when()
                            .post("/login")
                        .then()
                            .statusCode(200);
                    
                

In the first example, `search=test` and `page=2` are query parameters, while in the second example, `username` and `password` are form parameters.

Conclusion

REST Assured provides a variety of methods to handle parameters efficiently. Depending on the API design and requirements, you can use `queryParam`, `pathParam`, `formParam`, or the generic `param` method to make your API requests dynamic and precise.

Testing the Event Management API with Rest Assured

This guide demonstrates how to test all the provided endpoints using Rest Assured. The base URL for the API is http://apitesting.shariqsp.com:8080/.

1. GET /api/events/{id} (Get Event by ID)

Parameter Type: Path Parameter
The {id} is passed as a path parameter to fetch a specific event by its unique identifier. For example, /api/events/1 retrieves the event with ID 1.

              
              @Test
              public void testGetEventById() {
                given()
                      .pathParam("id", 1)
                  .when()
                      .get("http://apitesting.shariqsp.com:8080/api/events/{id}")
                  .then()
                      .statusCode(200)
                      .body("id", Matchers.equalTo(1))
                      .log()
                      .all();
              }
              
                

2. GET /api/events/searchByName (Get Events by Name)

Parameter Type: Query Parameter
The name parameter is sent as a query parameter in the URL to filter events by their name. For example, /api/events/searchByName?name=Tech%20Conference retrieves events named "Tech Conference."

              
                @Test
                public void testSearchEventsByName() {
                   given()
                        .queryParam("name", "Updated Tech Conference")
                    .when()
                        .get("http://apitesting.shariqsp.com:8080/api/events/searchByName")
                    .then()
                        .statusCode(200)
                        .body("name[0]", Matchers.equalTo("Updated Tech Conference"))
                        .log()
                        .all();
                }
              
                

3. GET /api/events/searchByLocation (Get Events by Location)

Parameter Type: Query Parameter
This endpoint uses a query parameter location to filter events by their location. For example, /api/events/searchByLocation?location=San%20Francisco.

              
                @Test
                public void testSearchEventsByLocation() {
                    given()
                        .queryParam("location", "Los Angeles")
                    .when()
                        .get("http://apitesting.shariqsp.com:8080/api/events/searchByLocation")
                    .then()
                        .statusCode(200)
                        .body("location[0]", Matchers.equalTo("Los Angeles"))
                        .log()
                        .all();
                }
              
                

4. POST /api/events (Create a New Event)

Parameter Type: JSON Body
In the POST request, the event name and location are sent as a JSON body. For example: {"name": "Tech Conference", "location": "San Francisco"}.

              
                @Test
                public void testCreateEvent() {
                    given()
                        .contentType("application/json")
                        .body("{\"name\": \"Updated Tech Conference\", \"location\": \"Los Angeles\"}")
                    .when()
                        .post("http://apitesting.shariqsp.com:8080/api/events")
                    .then()
                        .statusCode(200)
                        .body("name", Matchers.equalTo("Updated Tech Conference"))
                        .body("location", Matchers.equalTo("Los Angeles"))
                        .log()
                        .all();
                }
              
                

5. PUT /api/events/{id}/name (Update Event Name)

Parameter Type: Path Parameter and Form Parameter
In the PUT request, the {id} is passed as a path parameter to identify the event to be updated, and the new name is passed as a form parameter. For example: name=Updated%20Tech%20Conference.

              
                @Test
                public void testUpdateEventName() {
                   given()
                        .contentType("application/x-www-form-urlencoded")
                        .pathParam("id", 1)
                        .formParam("name", "Updated Tech Conference")
                    .when()
                        .put("http://apitesting.shariqsp.com:8080/api/events/{id}/name")
                    .then()
                        .statusCode(200)
                        .body("name", Matchers.equalTo("Updated Tech Conference"))
                        .log()
                        .all();
                }
              
                

6. PUT /api/events/{id}/location (Update Event Location)

Parameter Type: Path Parameter and Form Parameter
Similar to the event name update, the {id} is passed as a path parameter, and the new location is passed as a form parameter. For example: location=New%20York.

              
                @Test
                public void testUpdateEventLocation() {
                    given()
                        .contentType("application/x-www-form-urlencoded")
                        .pathParam("id", 1)
                        .formParam("location", "New York")
                    .when()
                        .put("http://apitesting.shariqsp.com:8080/api/events/{id}/location")
                    .then()
                        .statusCode(200)
                        .body("location", Matchers.equalTo("New York"))
                        .log()
                        .all();
                }
              
                

7. DELETE /api/events/{id} (Delete an Event)

Parameter Type: Path Parameter
The {id} is passed as a path parameter to identify the event to be deleted. For example: /api/events/1 deletes the event with ID 1.

              
              @Test
              public void testDeleteEventById() {
                  RestAssured.given()
                      .pathParam("id", 1)
                  .when()
                      .delete("http://apitesting.shariqsp.com:8080/api/events/{id}")
                  .then()
                      .statusCode(200)
                      .body("message", Matchers.equalTo("Event deleted successfully."))
                      .log()
                      .all();
              }