Handle HTTP PATH Parameters in Get Request using REST Assured

Profile picture for user devraj

Also known as Template parameters, path parameters are placed within the path of an endpoint just before the query string, and they provide developers with an effective way to parameterize resources. Path parameters are used to identify a resource uniquely.

Example Code

package com.pb.rest.presentation;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.response.Response;

import static io.restassured.RestAssured.given;

public class ReqRes 
{
	String id = "1";
	
	@BeforeClass
	public void setup()
	{
		RestAssured.baseURI = "https://reqres.in/";
		RestAssured.basePath = "api/users";
	}
	
	@Test
	public void pathParametersExample()
	{
		Response res = 
		given()
			.pathParam("id", id)
		.when()
			.get("{id}");
		
		System.out.println(res.body().prettyPrint());
	}
}

Here Response is an interface. To get the Response object from the HTTP Request then, once we have the Response object we can easily get its body as a String. Here, body will return  response body. Pretty-print the response body if possible and return it as string. Mainly useful for debug purposes when writing tests. Pretty printing is possible for content-types JSON, XML and HTML.