Hire QA – Specialized in QA Recruitment, Technical Interviews and Testing Solutions

Querying RequestSpecification In Rest Assured

Category: Rest Assured

To query or retrieve details from RequestSpecification, Rest Assured provides a class named SpecificationQuerier. This class has a static method called query(RequestSpecification specification) which returns reference of QueryableRequestSpecification interface. This interface has methods to retrieve request details such as getBasePath(), getBody() etc.

                // Creating request specification using given()
		RequestSpecification request1= RestAssured.given();
		// Setting Base URI
		request1.baseUri("https://hireqa.co.in")
		// Setting Base Path
		.basePath("/register")
		.body(JsonBody)
		.header("header1", "headerValue1")
		.header("header2", "headerValue2");

                // Querying RequestSpecification
		// Use query() method of SpecificationQuerier class to query 
		QueryableRequestSpecification queryRequest = SpecificationQuerier.query(request1);
		
		// get base URI
		String retrieveURI = queryRequest.getBaseUri();
		System.out.println("Base URI is : "+retrieveURI);
		
                // get base Path
		String retrievePath = queryRequest.getBasePath();
		System.out.println("Base PATH is : "+retrievePath);
		
		// get Body
		String retrieveBody = queryRequest.getBody();
		System.out.println("Body is : "+retrieveBody);

                // Get Headers
		Headers allHeaders = queryRequest.getHeaders();
		System.out.println("Printing all headers: ");
		for(Header h : allHeaders)
		{
			System.out.println("Header name : "+ h.getName()+" Header value is : "+h.getValue());
		}

Leave a Reply

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