如何在 SoapUI 中自动从 Rest API GET 响应下载附件

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

我正在使用 SoapUI 5.5.0,我正在尝试从 Rest API GET 响应自动下载 .xls 附件。 它没有出现在回复的附件选项卡中。

**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

在此之后,响应有一堆不可读的字符。

如果我查看 XML 选项卡,我会得到:

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

在这里添加这个,因为我无法在下面的感谢评论中获得可读的格式。

我在 response.getProperty('Content-Disposition').split('=')[1] 行有一个空错误。

由于我之前在测试用例中生成并存储了导出的名称,所以我得到 属性 然后使用它。

这是我的结尾:

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

响应的正文文件。你必须提取它,像这样:

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)