1

I am trying to capture the raw response for REST (POST) API call into a file using groovy Script.

I can see the response as below in RAW, but when file is produced it is blank.

REST Response:

HTTP/1.1 401 Unauthorized
content-length: 0
Date: Tue 12 jul 2016 12:12:12gmt
WWW-Autheticate: OAuth
Server: Jetty (8.1.17V20150415)

I am using SOAP UI version 5.2.

Any help appreciated.

Groovy Script:

def Date startTime = new Date()
File it=new File("Result") 
def cur_Time = startTime.getMonth()+1 + "_" + startTime.getDate()
cur_Time = cur_Time + "_" + startTime.getHours() + startTime.getMinutes() +startTime.getSeconds()
def fileName = it.name + "_" + cur_Time
//Request File
def myXmlRequest="C:\\ConnectivityResults\\"+ "Rest_Request" + fileName+".xml"
def request=context.expand('${Testcasename#Request}')
def req = new File (myXmlRequest)
req.write(request,"UTF-8")

//Response File
def myXmlResponse="C:\\ConnectivityResults\\"+ "Rest_Response" + fileName+".xml"
def response=context.expand('${Testcasename#Response}')
def res = new File (myXmlResponse)
res.write(response,"UTF-8")
albciff
  • 18,112
  • 4
  • 64
  • 89
Geeme
  • 395
  • 2
  • 6
  • 18

2 Answers2

1

The problem isn't probably in your Groovy script, the problem is simply that your request is incorrect and nothing is returned as response. Based on the http-headers you show in the question:

HTTP/1.1 401 Unauthorized 
content-length: 0 
Date: Tue 12 jul 2016 12:12:12gmt 
WWW-Autheticate: OAuth 
Server: Jetty (8.1.17V20150415)

You're receiving an 401 Unauthorized response instead of 200 OK, and based on the Content-lenght which is 0. It's normal that your response is blank, so there is no content to save in file.

EDIT BASED ON COMMENT

If you want also to save the http-headers in a file, you can add the follow snippet to your Groovy script:

def fileName = ...
// http-headers file
def httpHeadersFilePath ="C:/ConnectivityResults/Rest_Request${fileName}.txt"
def ts = testRunner.testCase.getTestStepByName('Testcasename')
def headers = ts.getTestRequest().response.responseHeaders
def httpHeaderFile = new File(httpHeadersFilePath)
httpHeaderFile.text = ''

headers.each { key, value ->
   httpHeaderFile.append("${key}:${value}\n",'UTF-8')
}

Hope it helps,

albciff
  • 18,112
  • 4
  • 64
  • 89
  • Thanks albciff. It is clear now, thanks for very detailed answer. – Geeme Jul 13 '16 at 13:42
  • Is there any way to capture the response(as per above) and put it in a file? – Geeme Jul 13 '16 at 13:44
  • @Geeme what did you mean? In case that response is blank, save for example the `http-headers`? – albciff Jul 13 '16 at 13:44
  • Hey albciff, Yes, whatever content appears in RAW, save that to file. for ex : HTTP/1.1 401 Unauthorized content-length: 0 Date: Tue 12 jul 2016 12:12:12gmt WWW-Autheticate: OAuth Server: Jetty (8.1.17V20150415) – Geeme Jul 13 '16 at 14:55
  • @Geeme I update my answer explaining how to add the `http-headers` from the response to a file `:)`. – albciff Jul 13 '16 at 15:29
0

Sorry about the late...

There's a simple way to take it and record in a file, using Groovy Script on your SoapUI:

#Take the Raw Request into a variable "request":
def request = context.expand( '${Request 1#RawRequest}' )
#Take the Raw Response into a variable "response":
def response = context.expand( '${Request 1#Response}' )

#Create and fill a file "MyFile.json" whit the variables values:
new File( "C:/foo/bar/MyFile.json" ).write( request + response, "UTF-8" )

Hope that's useful.

SnowBG
  • 89
  • 1
  • 2
  • 12