'How can I use Basic Auth in my RestAssured spec builder class

I am creating a new framework for restassured for Api testing.Just to optimize my test class I am using specbuilder class and created Utils class in another package.

This is what I am trying to do in Utils class:

public class Utils {
    
    RequestSpecification req;
     public RequestSpecification requestSpecification() {
         
         RestAssured.baseURI = "https://api-stgmars.com";
         req = new RequestSpecBuilder().setBaseUri("https://api-stgmars.com").setContentType(ContentType.JSON)
                 .build();
                 return req;     
     }
}

The problem here is I am not sure how to add basic auth . In my test class Basic auth is mandatory. which is something like this:

public static void createUser() {
                  
        RestAssured.baseURI = "https://api-stgmars.com";

        String response = given().auth() // Storing response body for user creation
                .basic(apikey, app_Secret).header("Content-Type", "application/json")

}

Please suggest how can I add Basic auth in my specbuilder class as there is no inbuilt method i found to do so.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source