Forum Discussion

ktimbal-LNR's avatar
21 days ago

Proper Way of Creating API Interface Using Script

Hi, I would like to know the proper way of creating an API service using groovy script. I am using the code below, but when creating for a REST Request it seems that the endpoint is not showing even if I pass a value on it. I cannot also add environment and authorization profile to the interface. I also noticed when creating the interface manually the specification is in the form of JSON vs WADL when created via script. Any help is very appreciated, many thanks!

Edit

I am using version 3.59.0.

import com.eviware.soapui.impl.rest.RestServiceFactory
import com.eviware.soapui.impl.rest.RestRequestInterface

def project = context.testCase.testSuite.project
def endpoint = "https://sample.endpoint.com"

if (!project.getInterfaceByName("NewRESTService")) {
	def restService = project.addNewInterface("NewRESTService", RestServiceFactory.REST_TYPE)
	restService.addEndpoint(endpoint)
	
	def resource = restService.addNewResource("NewResource", "/NewResource")
	
	def method = resource.addNewMethod("Method 1")
	method.method = RestRequestInterface.HttpMethod.POST

    def request = method.addNewRequest("Request 1")
    request.mediaType = "application/json"
      
    // Following line of codes are tested to pass endpoint but to no avail
    request.setEndpoint(endpoint)
    request.endpoint = endpoint
    request.setPropertyValue("Endpoint", endpoint)
    log.info request.getEndpoint()
    //---END---//

    request.authProfile = "BasicAuthProfile"
    log.info request.getAuthProfile()
}

 

1 Reply

  • JohnMonahan's avatar
    JohnMonahan
    New Contributor

    Hi ktimbal,

    Cool use case. You’re very close — the script you’ve written is on the right track. There are just a few ReadyAPI behaviors that explain what you’re seeing, plus small tweaks you can make.

    1) Endpoint not showing

    When an Environment is active, the endpoint you set in the script is saved but won’t appear in the UI unless that environment also defines it. You can switch to No Environment, or keep an environment active and set the endpoint inside it.

    Example A – make the endpoint visible with no environment:

    project.setActiveEnvironment(null)

    request.setEndpoint("https://sample.endpoint.com")

    Example B – keep an environment active and map the endpoint inside it:

    def env = project.getEnvironmentByName("Local")

    if (env == null)

        env = project.addNewEnvironment("Local")

    env.setRestServiceEndpoint(restService, "https://sample.endpoint.com")

    project.setActiveEnvironment("Local")

    2. Auth profile not applying

    The line request.authProfile = "BasicAuthProfile" only works if that profile already exists at the project level. Some older versions (like 3.59) may not expose an API for creating Basic profiles from Groovy. If that’s your case, set the Authorization header directly (same result on the wire):

    import com.eviware.soapui.support.types.StringToStringsMap

    def creds = "myuser:mypassword".getBytes("UTF-8").encodeBase64().toString()

    def headers = new StringToStringsMap(request.getRequestHeaders())

    headers.put("Authorization", "Basic " + creds)

    if (!headers.containsKey("Content-Type")) {

        headers.put("Content-Type", "application/json")

    }

    request.setRequestHeaders(headers)

     

    3.WADL vs JSON (OpenAPI)

    If you don’t bind an OpenAPI definition, the interface appears as WADL. If you want JSON/YAML in the UI, bind your spec before adding resources:

    try {

        restService.setDefinition("file:///C:/path/to/openapi.json", false)

        restService.importDefinition(false)

    } catch (t) {

        log.warn("OpenAPI bind failed: " + t.message)

    }

     

    Notes:

    I tested these changes on ReadyAPI 3.62; 3.59 is very similar, but some method names may differ slightly. For general reference, here’s the ReadyAPI scripting guide:

    https://support.smartbear.com/readyapi/docs/en/test-apis-with-readyapi/scripting.html

    Hope this helps!

    John Monahan

    SmartBear Solutions Engineer