JimLin
12 years agoOccasional Contributor
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.
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.
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.
**************
EDIT:
I should add that I am using the latest version of SOAPUI Pro.
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}' )
log.info(response)
def responseAsXml = context.expand( '${createAccount#ResponseAsXml}' )
log.info(responseAsXml)
def request = context.expand( '${createAccount#Request}' )
log.info(request)
def rawRequest = context.expand( '${createAccount#RawRequest}' )
log.info(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()
log.info(header)
def location = header["Location"]
log.info(location)
def pattern = ~/\w*$/
def matcher = (location =~ pattern)
log.info(matcher)
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.
**************
EDIT:
I should add that I am using the latest version of SOAPUI Pro.