Make Selenium Easy

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

Introduction

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:-

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.

How to put parameters in URL?

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.

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

How to pass parameter value in URL?

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.

Using pathParam() method

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:-

Program example
Output

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.

Example program

You will see the same output as above.

Using inline path parameter

We can use inline path parameter as shown below:-

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.

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.

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.

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

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

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

by Amod Mahajan time to read: 3 min
0
%d bloggers like this: