Rest Assured – Query Parameter vs. Path Parameter

What is Query Parameter in Rest API?

We can use query parameters to control what data is returned in endpoint resources. It appears at the end of the URL after the question mark (?) and helps us to control the set of items and properties in responses, and the order of the items returned.

Consider the following GitHub API url:

https://api.github.com/user/repos?sort=created&direction=desc

This will list all repositories for an authenticated user but the response properties will be sorted by repository created and in descending order.

There are two query parameters defined in the API url:

– sort=created (sort repositories by created date)

– direction=desc (sort in descending order)

What is Path Parameters in Rest API?

Path parameters are variables in a URL path. They are used to point to a specific resource within a collection. We can define multiple PATH parameters and each of them is  represented by a curly brace {}.

Consider the following GitHub API url:

https://api.github.com/users/:username/repos

This will list all public repositories for the specified user with the value username.

:username is the Path parameter in the above url.

How to pass Query Paramter and Path Parameter using Rest Assured?

public class GithubAPITest {

    String apiToken = "your api token";
    String url = "https://api.github.com";

    //To pass query parameters in the API URL
    @Test
    public void verifyRepos(){
        String url_new = url + "/user/repos";
        given().auth().oauth2(apiToken)
                .queryParam("sort","created")
                .queryParam("direction","desc")
                .when().get(url_new).then().log().body();
    }

    //To pass path parameters in the API URL
    @Test
    public void getRepoForSpecificUser(){

        String apiURL = url + "/users/{username}/repos";
        given().auth().oauth2(apiToken)
                .pathParam("username","qascript20")
                .when().get(apiURL).then().log().body();
    }
}

 

Share

Bijan Patel

Founder & Creator of QAScript | 12+ years of IT Experience | Full Stack Automation Engineer | Blogger | Trainer

You may also like...

1 Response

  1. Sunil Kirangi says:

    Hello Bijan,
    I am interested in enrolling for BDD framework course.
    Is it possible to take classes online.
    Thanks
    Sunil Kirangi

Leave a Reply

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