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

 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.

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.

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.

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
@Test
	public void pathVariable1()
	{
		RestAssured
			.given()
				.log()
				.all()
				.baseUri("https://restful-booker.herokuapp.com/")
				.basePath("{resourcePath}")
			    .pathParam("resourcePath", "booking")
			.when()
				.get()
			.then()
				.log()
				.all();
	}
Output
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.

Example program
@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.

Using inline path parameter

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

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.

Leave a Reply

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