Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

How can I get values from REST path and return it as a string in the response?

pUser
pUser Posts: 6

For example, I have somehost.com/restapi/v1/value1 and I want it to return value1.

I set up REST URL Data Bank and set "Path - 0" to value1.

In one of the attribute in the response, I use this Groovy script...

import com.parasoft.api.*;

boolean customAssertion(Object input, ScriptingContext context) {
String value = context.getValue("Generated Data Source", "value1");
Application.showMessage("Value from data source is " + value)
Application.showMessage("Value from SOAP response is " + input.toString())
return input.toString().contains(value);
}

But I am getting this error..

Unable to invoke user method customAssertion(java.lang.Object, com.parasoft.api.ScriptingContext). Argument count should be 0 or 1 instead of 2.

Would anyone be able point me on how to resolve this issue?

Comments

  • williammccusker
    williammccusker Posts: 672 ✭✭✭

    Hi,

    Can you share more information about where this script is? Depending on the location scripting methods are passed different numbers of arguments. In this case it is saying it expecting to find a method that only takes a single input or none.

    Have you tried changing you method to take a single input? The scripting context to set if that works?

  • pUser
    pUser Posts: 6

    The script locates in an attribute in the response. I tried

    import com.parasoft.api.*;
    import soaptest.api.*;

    String doofus(ExtensionToolContext context) {
    String value = context.getValue("value1");
    Application.showMessage("Value from data source is " + value)
    Application.showMessage("Value from SOAP response is " + input.toString())
    return input.toString().contains(value);
    }

    I don't see any errors, but I am not seeing the value in the attribute in the response.

  • williammccusker
    williammccusker Posts: 672 ✭✭✭

    Hi,

    I think the cause is the "getValue" was suppose to still the method with the data source name.

    getValue("Generated Data Source", "value1")

    Is there a reason you changed that in the second script?

    Here is link to the scripting reference
    https://docs.parasoft.com/display/SOAVIRT9103CTP310/Extensibility+and+Scripting+Basics

  • pUser
    pUser Posts: 6

    I was just trying all the different options. I also tried this

    import com.parasoft.api.*;
    import soaptest.api.*;

    String doofus(ExtensionToolContext context) {
    String value = context.getValue("Generated Data Source", "value1");
    Application.showMessage("Value from data source is " + value)
    Application.showMessage("Value from SOAP response is " + input.toString())
    return input.toString().contains(value);
    }

    I still don't see any value coming back.

  • pUser
    pUser Posts: 6

    I was able to use ${value1} and set that in fixed and it works.

  • williammccusker
    williammccusker Posts: 672 ✭✭✭

    Hi,

    Is your use case that you want to validate that "value1" from the path is present in the request message that was received and use that as part of the response payload?

    I have attached an example that does that by chaining an extension tool to the incoming request payload after the data bank. I then output the result into another data bank to create an "assertion" column that I use to parameterize the value of an attribute in the response payload.

  • pUser
    pUser Posts: 6

    I didn't need validation, I just want value1 from the path is present in the request message that received and use that as part of the response payload. Thank you for the example. It seems that It's missing ${BASE_URL}. Would you be able to provide StubService service?

  • williammccusker
    williammccusker Posts: 672 ✭✭✭

    Hi,

    You can just remove that setting. That is for if you have a WSDL and want the response to be based on the definition. I didn't actually use it the example. I think I probably copied something from some where else and forgot that setting was in there.

    In the "Definition" tab just delete all the text in the "WSDL URL" text box.

  • pUser
    pUser Posts: 6

    Got it. Thank you so much for your help.

  • williammccusker
    williammccusker Posts: 672 ✭✭✭

    Happy to help!