Rest Assured HTTP Request with Query Parameters

In this tutorial on Rest Assured, I am going to share with you how to create a test case which sends HTTP Get Request and includes two Query String Request Parameters.

Let’s assume we need to test a RESTful Web Service Endpoint which returns a list of users. The URL to our RESTful Web Service endpoint will look like the one below and will contain two Query String Request Parameters:

  • page and
  • limit

Where the page Query String Request Parameter is to provide a page number for which we would like to return a list of users and the limit request parameter is to limit the number of users returned per page to a certain number.

http://localhost:8080/users?page=1&limit=50

Rest Assured queryParam()

To include Query String Request Parameters into our HTTP GET request we will need to use the queryParam(). The queryParam() function accepts key-value pair where the key is the name of the request parameter and the value is the value of the request parameter.

queryParam("page", "1")
queryParam("limit", "50")

HTTP GET Request with Query Parameters

Below is an example of Rest Assured test case which sends HTTP GET Request and includes two query string request parameters: the page and the limit:

    @Test
    public void testGetListOfUsers() {

        Response response = given()
                .header("Authorization", authorizationHeader)
                .accept(JSON)
                .queryParam("page", "1")
                .queryParam("limit", "50")
                .when()
                .get(CONEXT_PATH + "/users")
                .then()
                .statusCode(200)
                .contentType(JSON)
                .extract()
                .response();

        String jsonBody = response.getBody().asString();

        try {
            JSONArray usersArray = new JSONArray(jsonBody);
            assertNotNull(usersArray);
            assertTrue(usersArray.length() > 0);
        } catch (JSONException ex) {
            fail(ex.getLocalizedMessage());
        }

    }

Conclusion

In conclusion, Rest Assured provides a straightforward and efficient way to include query parameters in HTTP requests. By utilizing the queryParam() method and the appropriate HTTP request methods, developers can easily send GET requests with query parameters.

Looking to strengthen your skills in testing RESTful web services? Explore our in-depth tutorial on the Testing Java Code page, where we dive into the powerful capabilities of REST Assured. Learn how to write expressive and reliable tests for your RESTful APIs.

Below is a list of video courses that teach Rest Assured. Have a look at them and see if you like one of them.


Leave a Reply

Your email address will not be published. Required fields are marked *