Forum Discussion

ranujn's avatar
ranujn
Contributor
6 years ago
Solved

How to get Endpoint from the Request

I wanna to get full Endpoint from the request. I passing endpoints dynamically for each step that I need to record it.

I tried below options to get complete url (Constant one + dynamically passed) but its just giving me only the constant endpoint

1) def endpoint = (testRunner.testCase.testSteps["GET"].testRequest.response.rawResponseData)
new String(endpoint)

The Output is something like this - [B@7f0b1367

2) def endpoint = (testRunner.testCase.getTestStepByName("GET").getTestRequest().getResponse().getRawRequestData())

new String(endpoint)

The output is something like this - [B@38b1c3e8
3) endpoint = testRunner.testCase.getTestStepByName("GET").getHttpRequest().getEndpoint()

Output is something like this - https://customer-tst.api-pa.com

4) endpoint = context.testCase.getTestStepByName('GET').getPropertyValue("Endpoint")

Output is something like this - https://customer-tst.api-pa.com

So basically I am failed to get complete URL. Can someone please help me to get a solution for this.

  • ranujn 

     

    You gave me this response right?

     

    {
    "data" : {
    "token" : "sidayf8r4jhrdsuf97adif09rujkfhw.8regiu6sy9s7d6fsa87s87439ykhkhfjdsbfsdf"
    }
    }

    And I provided the code based on that. If you are getting the error. In that case, "data" attribute is not there in your response. You can pass the reponse to JsonSlurper. please ad few more lines of code:-

     

     

     

    import groovy.json.JsonSlurper
    
    def slurper = new groovy.json.JsonSlurper()
    def response = context.expand('${API_Name#Response}') def slurp = slurper.parseText(response)
    def token = slurp.data.token //Considering "data" and "token" as JSON attributes.

     

     

17 Replies

  • avidCoder's avatar
    avidCoder
    Super Contributor

    If you have REST endpoint, it consists of Resource path also with the endpoint. So Did you mean that you want thre resource path also added to endpoint?

     

    If it is like that, then please try below code:-

     

    String endPointUrl = testStep.getHttpRequest().getEndpoint();
    String path = testStep.getHttpRequest().getPath()
    String actualURL = endPointUrl+path
    
    //Print actualURL
    • ranujn's avatar
      ranujn
      Contributor

      Yeah, You got me correctly. 

       

      I used the above script in the Groovy script to get Rest API endpoint plus resource path. but I am getting below error message. Am I doing something wrong here?

      Tue Jan 22 16:39:16 AEDT 2019:ERROR:An error occurred in the script of the Groovy Script test step [Loop End]:
      Tue Jan 22 16:39:16 AEDT 2019:ERROR:groovy.lang.MissingPropertyException: No such property: testStep for class: Script20
      groovy.lang.MissingPropertyException: No such property: testStep for class: Script20
      at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
      at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52)
      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307)
      at Script20.run(Script20.groovy:1)
      at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:98)
      at com.eviware.soapui.support.scripting.groovy.SoapUIProGroovyScriptEngineFactory$SoapUIProGroovyScriptEngine.run(SoapUIProGroovyScriptEngineFactory.java:82)
      at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:156)
      at com.eviware.soapui.impl.wsdl.panels.teststeps.GroovyScriptStepDesktopPanel$RunAction$1.run(GroovyScriptStepDesktopPanel.java:329)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)

       

      • avidCoder's avatar
        avidCoder
        Super Contributor
        //Considering that testStep is the object which means you need to pass your URL there.
        
        def testStep = testRunner.testCase.getTestStepByName("YOUR REST REQUEST NAME")

        else directly use this code:-

         

        String endPointUrl = testRunner.testCase.getTestStepByName("YOUR REST REQUEST NAME").getHttpRequest().getEndpoint();
        String path = testRunner.testCase.getTestStepByName("YOUR REST REQUEST NAME").getHttpRequest().getPath()
        String actualURL = endPointUrl+path

        Accept as solution, if this helps you out and don't forget to give Kudos. :)