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());
        }

    }

I hope this short tutorial was helpful for you!

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