REST Assured Tutorial 19 – Default Host And Port in Rest Assured

As a part of End to End REST Assured Tutorial , in this post We will learn about “Default Host and Port in Rest Assured”.

A port is a communication endpoint where a service is available. For example if we install Jenkins on a server or local , we start it at a specific port and using that port we access Jenkins. So if you have some services running on a specific port, you need to pass that in URI so that request could hit to correct endpoint. Final BaseURI is created by appending port with colon as prefix .

Example:- http://localhost:8080/

Before going further, let’s hit a request and observe output:-

package RestAssuredConcepts;

import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

public class DefaultHostAndPortExample {
	
	
	public static void main(String[] args) {
		
		// Hitting a GET request without setting any base URI and Path
		RestAssured
		   .given()
		   // Logging all details
		   .log()
		   .all()
		.when()
		   .get();
		   	
	
	}

}

Output:-

In output, you will see a line as below:-

Request URI:	http://localhost:8080/

By default REST assured assumes host localhost and port 8080 when doing a request. It means if we not provide any host and port, it will take default values. It is an interview question.

We can always override it to pass desired URI. We have seen already setting a BaseURI earlier. Setting up port is also similar.

We can setup a port in below ways:-

Using RequestSpecification

BDD Style:-

RequestSpecification interface has a method named port(). We can use that to set a desired port number.

// Using BDD
				RestAssured
				.given()
					.baseUri("https://restful-booker.herokuapp.com")
					.basePath("/ping")
					.port(8181)
				.when()
					.get();

Non-BDD Style:-

// Creating request specification using given()
		RequestSpecification request1= RestAssured.given();
		// Setting Base URI
		request1.baseUri("https://restful-booker.herokuapp.com");
		// Setting Base Path
		request1.basePath("/ping");
		request1.port(8181);

Using RequestSpecificationBuilder

RequestSpecification req2= new RequestSpecBuilder()
			.setBaseUri("https://restful-booker.herokuapp.com")
			.setBasePath("/ping")
			.setPort(8181)
			.build();

Using static property of RestAssured class

This way will set port number for all subsequent request.

// Using RestAssured static property
RestAssured.port = 9191;

You can clone/download example repo here.

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

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

1 thought on “REST Assured Tutorial 19 – Default Host And Port in Rest Assured

Leave a Reply

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