Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 02/19/2025 By admin

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

Uncategorized

Post navigation

Previous Post: TestNG Tutorials 40: Groups of Groups or MetaGroups in TestNG
Next Post: API Testing Tutorial Part 13 – What is a Payload in an API?

Related Posts

September 28, 2017 – Make Selenium Easy Uncategorized
TestNG Tutorials 48: How to Pass Parameters of Different Datatypes in TestNG Uncategorized
TestNG Tutorials – Page 7 Uncategorized
image – Make Selenium Easy Uncategorized
mahajan Uncategorized
subPackages – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark