Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 62 – How To Use Path or URL Parameters In Rest Assured

Posted on 03/16/2025 By admin

As a part of the End to End REST Assured Tutorial, in this post, we will learn to pass Path parameters or path variables or URL parameters in URI using Rest Assured.

We can parameterize URL to make them dynamic, readable, and reusable. For example:-

 https://restful-booker.herokuapp.com/auth https://restful-booker.herokuapp.com/booking https://restful-booker.herokuapp.com/ping 

If you observe the above URLs, “https://restful-booker.herokuapp.com/” is common in all three URLs, and only base paths are changing. We can create a parameterized URL that can be reused as required by providing value.

A parameter is a key-value pair. A URL will have parameters whose value can be provided in multiple ways. Let’s learn to make the above URL parameterized.

https://restful-booker.herokuapp.com/{resourcePath}
https://restful-booker.herokuapp.com/{resourcePath}/{bookingID}

We need to pass a parameter key name in curly braces as shown above. We can pass more than one parameter.

If we make a URL parameterized then it is our responsibility to pass values for those. Otherwise, it will consider the URL as it is. There are multiple ways to pass a value to parameters.

pathParam() method takes two string parameters. The first parameter is the parameter name and another parameter is the parameter value. An example is shown below:-

@Test
        public void pathVariable1()
        {
                RestAssured
                        .given()
                                .log()
                                .all()
                                .baseUri("https://restful-booker.herokuapp.com/")
                                .basePath("{resourcePath}")
                            .pathParam("resourcePath", "booking")
                        .when()
                                .get()
                        .then()
                                .log()
                                .all();
        }
Request method: GET
Request URI:    https://restful-booker.herokuapp.com/booking
Proxy: 
Request params: 
Query params:   
Form params:    
Path params:    resourcePath=booking
Headers: Accept=*/*
Cookies: 
Multiparts: 
Body: 
HTTP/1.1 200 OK
Server: Cowboy
Connection: keep-alive
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 162
Etag: W/"a2-gzy3QBBwVx4LfnY0IjxFXn1y8jQ"
Date: Sat, 09 Jan 2021 07:24:05 GMT
Via: 1.1 vegur [ { "bookingid": 2 }, { "bookingid": 10 }, { "bookingid": 1 }, { "bookingid": 5 }, { "bookingid": 7 }, { "bookingid": 3 }, { "bookingid": 4 }, { "bookingid": 8 }, { "bookingid": 9 }, { "bookingid": 6 }
]

You can see above output that parameter “resourcePath” was replaced by “booking” at run time.

We can also pass the complete parameterized URL in HTTP method call (get(), post() etc) instead of using baseURI and basePath.

@Test
        public void pathVariable2()
        {
                RestAssured
                        .given()
                                .log()
                                .all()
                            .pathParam("resourcePath", "booking")
                        .when()
                                .get("https://restful-booker.herokuapp.com/{resourcePath}")
                        .then()
                                .log()
                                .all();
        }

You will see the same output as above.

We can use inline path parameter as shown below:-

@Test
        public void pathVariable3()
        {
                RestAssured
                        .given()
                                .log()
                                .all()
                        .when()
                                .get("https://restful-booker.herokuapp.com/{resourcePath}", "booking")
                        .then()
                                .log()
                                .all();
        }

Observe above that get() method takes two arguments now. In fact, the second argument i.e. “booking” is the value for path parameter “resourcePath”. This is called the unnamed path parameter as in the above case value will be set based on an index, unlike pathParam() method.

@Test
        public void pathVariable4()
        {
                RestAssured
                        .given()
                                .log()
                                .all()
                        .when()
                                .get("https://restful-booker.herokuapp.com/{resourcePath}/{bookingId}", "booking",10)
                        .then()
                                .log()
                                .all();
        }

In the above example “booking” will be the value for the 0th index i.e. “resourcePath” and “10” will be the value of the 1st index i.e. “bookingId”.

You can mix named parameters and unnamed parameters as well but you need to be careful while mixing.

@Test
        public void pathVariable4()
        {
                RestAssured
                        .given()
                                .log()
                                .all()
                                .pathParam("resourcePath", "booking")
                        .when()
                                .get("https://restful-booker.herokuapp.com/{resourcePath}/{bookingId}",10)
                        .then()
                                .log()
                                .all();
        }

First, it will use pathParam() method to set values for path parameters, and then the remaining path parameters will be considered based on the index. In the above example, “resourcePath” will be set by pathparam() method, and “bookingId” will be set by inline parameter value i.e. 10.

Note – If you are using path parameters with baseURI() or RestAssured.baseURI, it will not work.

@Test
        public void pathVariable5()
        {
                RestAssured
                        .given()
                                .log()
                                .all()
                                .baseUri("https://restful-booker.herokuapp.com/{resourcePath}")
                            .pathParam("resourcePath", "booking")
                        .when()
                                .get()
                        .then()
                                .log()
                                .all();
        }

You will get an error on running the above program:-

java.net.URISyntaxException: Illegal character in path at index 37: https://restful-booker.herokuapp.com/{resourcePath}

You can download/clone the above sample project from here.

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: Interview Experience at Innoflexion Bangalore for JavaScript and API Automation Testing Profile ( Sep – 2019)
Next Post: REST Assured Tutorial 38 – How Getter & Setter methods Matter For Serialization Deserialization Using POJO

Related Posts

TestNG Tutorials 36: Can a Test Method Return a Value in TestNG? – Make Selenium Easy Uncategorized
API Testing Tutorial Part 10 – Introduction of SOAP (Simple Object Access Protocol) Uncategorized
css Uncategorized
image – Make Selenium Easy Uncategorized
Git Tutorial 23 – Git Stash Apply – How To Solve Merge Conflict Uncategorized
image – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark