Forum Discussion

Hoobajoob's avatar
Hoobajoob
Occasional Contributor
11 years ago

Extract substring from response body

I think this may be an xpath question, not sure.

I have a test case that exercises a REST method and the result returns a raw url in the message body, no xml or json. I believe it returns this same value in the HTTP location header. The url includes an id field, and I want to save just that id value and transfer it to a test case property. The length of the id field is variable (I can't count on it always being so many characters long).

Response body looks like:

http://<server>:<port>/objects/{id}

...where <server> and <port> are property values from an earlier data source step, and {id} is generated by the service, and has a different value after each request.

I have a property transfer step after the REST method, but I can't find anything obvious for doing this simple substring operation: give me everything after the last occurrence of '/' in the response body. I was hoping for regular expressions and capture groups, but I don't see any support for this kind of thing in SoapUI.

Does anybody know how to do this? Treat the response as xml and try using xpath to get this substring? I'm not an xpath expert, but I don't remember xpath being much use for substring operations like this where you don't know what the length of the string is, or the values of <server> and <port> at design time.
  • GiscardN's avatar
    GiscardN
    Frequent Contributor
    Hi,

    You could use a Groovy TestStep after your request to retrieve that id value from your response.
    If you isolate that URL as a string, then you can extract the id as follows:
    def string = "http://servername:3333/objects/abcded"
    def sub = string.substring(string.indexOf("objects/")+8, string.length())
    log.info sub //the value "abcded" will be assigned to the variable 'sub'

    Then you can store that value as a TestCase property. See this article:
    http://www.soapui.org/Scripting-Properties/tips-a-tricks.html#1-1-get-and-set-properties
    And you can access it in the subsequent steps of your TestCase.

    Regards,

    Giscard
    SmartBear Support
  • Hoobajoob's avatar
    Hoobajoob
    Occasional Contributor
    Thank you for the reply, Giscard!

    In the end, I did use a groovy script, like so:

    def response = context.expand( '${RestRequestTestStep#Response}' )
    def matches = response =~ /http:\/\/.*\/(\d+)$/
    return matches[0][1]


    ...where "RestRequestTestStep" is the name of the test step that executed the request. Also added a property transfer step to save the script result to a TestCase property.

    Thanks for helping out the newbies!