Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

groovy - How to automate downloading attachment from Rest API GET response in SoapUI

I am using SoapUI 5.5.0 and I am trying to automate the download of an .xls attachment from a Rest API GET response. It does not appear in the attachment tab of the response.

  • I tried adding "Enable MTOM | true" but the request stop working with it.
  • I tried some groovy scripts but I didn't get anything out of what I tried.
**RAW RESPONSE**

HTTP/1.1 201 
Set-Cookie: Design_Authorization=VeryLongToken; Max-Age=93600; Expires=Tue, 12-Jan-2021 22:33:22 GMT; Path=/Redacted; HttpOnly
Set-Cookie: JSESSIONID=bunchofnumbers; Path=/Redacted; HttpOnly
Content-Disposition: attachment; filename=SoapUI_Export_DD_20210111_153209.xls
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Type: application/vnd.ms-excel
Transfer-Encoding: chunked

After this, the response has a bunch of unreadable characters.

If I look at the XML tab I get this:

**XML RESPONSE**
<data contentType="application/vnd.ms-excel" contentLength="647680">0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAOwADAP7/CQAGAAAAAAAAAAAAAAAKAAAAAAAAAAAAAAAAEAAA/v///wAAAAD+////AAAAAAEAAACAAAAAAAEAAIABAAAAAgAAgAIAAAADAACAAwAAAAQAAIAEAAD///...it's very long

Adding this here since I could not get a readable format in my thank you comment below.

I had a null error on the response.getProperty('Content-Disposition').split('=')[1] line.

Since I generate and store the name of the export earlier in the testcase, I get the property and then use it.

This is what I ended with:

import org.apache.commons.io.FileUtils

def testStep = testRunner.testCase.testSteps['test step name']
def response = testStep.testRequest.response
assert response.getContentType() == 'application/vnd.ms-excel'
def data = response.getRawResponseBody()

// define filepath/name 
exportname = testRunner.testCase.getPropertyValue("exportName") 
reportfolder = (System.getProperty("user.home") + File.separatorChar + "Documents" + File.separatorChar); 
def filename = reportfolder + exportname +'.xls' 
def file = new File(filename) 
FileUtils.writeByteArrayToFile(file, data) `

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The body of the response is the file. You have to extract it, something like this:

import org.apache.commons.io.FileUtils

def testStep = testRunner.testCase.testSteps['test step name']
def response = testStep.testRequest.response
assert response.getContentType() == 'application/vnd.ms-excel'
def data = response.getRawResponseBody()

// define some filename
def filename = response.getProperty('Content-Disposition').split('=')[1]
def file = new File(filename)
FileUtils.writeByteArrayToFile(file, data)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...