Geeme
Geeme

Reputation: 415

SOAP UI - How to Capture REST raw Response in a file

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")

Upvotes: 2

Views: 4727

Answers (2)

SnowBG
SnowBG

Reputation: 89

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.

Upvotes: 0

albciff
albciff

Reputation: 18507

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,

Upvotes: 2

Related Questions