How to pass Query Parameters in Rest Assured?

This entry is part 7 of 14 in the series Rest Assured Tutorial

So far, we have already covered all major HTTP method operations using REST-Assured. If you haven’t gone through that, please check this link. Now, In this tutorial, we will see “How to pass a Query Parameter in GET Request using Rest Assured?”.

 

Simple representation of sending Query Parameter using RESTAssured...!!! Click To Tweet

In this tutorial, we are going to cover the below topics:

  1. What is Query Parameter?
  2. How to make a request with Query Parameter in Rest Assured?

 

Let’s begin:

1. What is Query Parameter?

Query Parameter is used to filter or sort the resources.

For example, In case, if we need to filter out the students based on their class or section, we are going to use ‘Query Parameter’.

GET /students?class=9&section=B

Sometimes there is a confusion between the Query Parameter and URI Parameter (or Path Parameter). ‘Path Parameter’ is basically used to identify a specific resource.

For example, In case, if we need to identify a student based on his/her rollNumber, we are going to use ‘Path Parameter’.

GET /student/{rollNumber}

 

2. How to make a request with Query Parameter in Rest Assured?

In this tutorial, we will use 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 ‘/users‘. And, if we try to access it, we will get output like below:

reqres_get_Techndeck

 

As you can see in the above screenshot, for ‘page: 1’, there are 3 different users present in the data section with id=1, 2, and 3.  Now, similar to this, page 2 has some entries, page 3 has some entries, and so on.

In this particular example, our objective is to query with page=2 and id=5 and let’s see what we will get.

The full-service URL with the endpoint is https://reqres.in/api/users?page=2&id=5

Here is the code to send the request with Query Parameters to the above-mentioned Service Endpoint:

 

Let’s try to understand the code:

1. Setting up Base URI

Base URI is the root address of the Resource. And, by this particular line of code, we are specifying to REST assured to use “reqres.in/api” as the root URL of the service.

2. Specifying the exact resource to look for and make a GET request to that resource

Here, using this code, we are looking to make a GET request to a resource that is “/users” and querying it with two parameters (page and id). Hence, the complete Service Endpoint would be “https://reqres.in/api/users?page=2&id=5” to which we are sending a GET request. And, the output of the GET call would be stored in the REST Assured ‘Response’ object.

Let’s run the above specified ‘GetRequestWithQueryParameters’ test class.

Eclipse Console Output:  

 

That’s it, it’s that simple to pass Query Parameter in REST Assured API: ?

 

Simple representation of GET Request with Query Parameters using RESTAssured...!!! Click To Tweet

If you like this post, please check out my other useful blog posts on Rest Assured:

Other Useful References:

Series Navigation<< How to send DELETE request using REST Assured(Updated)Serialization and De-Serialization using Rest Assured >>

Leave a Reply

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

Facebook Twitter Instagram YouTube

Apach_HttpClient_Tutorial_Popup_Techndeck