Forum Discussion

JimLin's avatar
Occasional Contributor
12 years ago

Extracting an account number from an HTTP raw response

I am struggling with this problem.

I have a Rest test step which creates an account. I can create the account successfully, but the only confirmation of this success I get is an 'HTTP 201 Created' response which is shown below:

HTTP/1.1 201 Created
Server: Apache-Coyote/1.1
Location: http://xx.xx.xx.xx:xxxx/account-api/res ... nt/12345AB
Content-Type: application/xml
Content-Length: 0
Date: Thu, 23 May 2013 14:59:42 GMT
Connection: close

I need to get the account number at the end of the Location:http... line, so I can then pass it in to the next step, so I can recall the account details and add them to a report. The only way I can see to get to it is to use a Groovy test step, but despite my best (or worst efforts ) my very limited knowledge of groovy fails me and I have found nothing that works. Even using the code below derived using the right click method in a Groovy step doesn't return anything, which i guess is because it is an HTTP response, rather than anything else.

def response = context.expand( '${createAccount#Response}' )

def responseAsXml = context.expand( '${createAccount#ResponseAsXml}' )

def request = context.expand( '${createAccount#Request}' )

def rawRequest = context.expand( '${createAccount#RawRequest}' )

I have tried the Groovy messageExchange and regex as an assertion and can select the Location line from the response, which is good. I have then tried a regular expression to select the account number. Using the RegEx helper plug in in notepad++ I can separate the account number, but when I try it in the assertion in SOAPUI it passes the assertion, but fails to return the account number.

def header = messageExchange.getResponseHeaders()
def location = header["Location"]
def pattern = ~/\w*$/

def matcher = (location =~ pattern)

Fri May 24 14:10:55 CEST 2013:INFO:Date : Thu, 23 May 2013 14:59:42 GMT
#status# : HTTP/1.1 201 Created
Content-Length : 0
Location : http://xx.xx.xx.xx:xx/account-api/rest/account/12345AB
Connection : close
Content-Type : application/xml
Server : Apache-Coyote/1.1

Fri May 24 14:10:55 CEST 2013:INFO:[http://xx.xx.xx.xx:xxxx/account-api/rest/account/12345AB]
Fri May 24 14:10:55 CEST 2013:INFO:java.util.regex.Matcher[pattern=\w*$ region=0,58 lastmatch=]

My concern is that even if I manage to separate it, I still won't be able pass it to the next step. I think it would work better as a Groovy test step, but as mentioned earlier, I failed miserably to make any progress on that option at all.

Any advice, guidance or assistance you can provide will be gratefully received.

I should add that I am using the latest version of SOAPUI Pro.

5 Replies

  • JimLin's avatar
    Occasional Contributor
    Hi Wehrmacht,

    Thanks for the reply. I'm not sure what you mean by 'Download all the xml resources'. There is no xml being displayed. the only results tab that is populated is the Raw one, all the rest are blank.

    • exams4success's avatar
      Occasional Visitor

      HTTP (hyper text transform protocol) this means that the website is unsafe.if the website is secure the HTTPs will be must  provides about HTTP detail knowledge and you can easily work from HTTP raw response.

  • JimLin's avatar
    Occasional Contributor
    Hi Wehrmacht,

    I had come to a similar conclusion and think it's something to do with the construction of the WADL, I need to get some time with the developer who created it.

    I have realised that there is another API call that makes the account number field available in the normal way, so I've gone down that route for the time being.

    I'll update this post if I find a solution.

  • There is no compile time Error nor Run time but when I execute the Junit, it gets executed in 0ms.
  • nmrao's avatar
    Champion Level 3

    Here is the groovy code which extracts the AccountID.
    Note that, i had to modify the url as the snippet you mentioned is malformed url and created a map manually to test for the intended value (you may remove map as you dont need it)

    def header = ['HTTP/1.1 201 Created':'',
    'Server': 'Apache-Coyote/1.1',
    'Location': '',
    'Content-Type': 'application/xml',
    'Content-Length': '0',
    'Date': 'Thu, 23 May 2013 14:59:42 GMT',
    'Connection': 'close']
    def location = header['Location']
    System.out.println('Location from headers is '+location)
    def pathStrings = location.toURL().path.split('/')
    def accountID = pathStrings[pathStrings.length - 1]
    System.out.println('Account ID '+accountID)

    Location from headers is
    Account ID 12345AB

    Now, how it needs to be used is upto you few suggestions though:
    1. You may add it as script assertion in your response
    2. Additionally you can add a line of code into the above script to set the Project/TestSuite/TestCase level property to the ACCOUNTID, for eg: project.setProperty('ACCOUNTID',accountID). Then access the value in any step, depends on where you set the property like ${#Project#ACCOUNTID}/${#TestSuite#ACCOUNTID}/${#TestCase#ACCOUNTID}.
    Is this what you are looking for?