【发布时间】:2021-01-12 15:42:58
【问题描述】:
我正在使用 SoapUI 5.5.0,我正在尝试从 Rest API GET 响应自动下载 .xls 附件。 它不会出现在响应的附件选项卡中。
- 我尝试添加“启用 MTOM | true”,但请求停止使用 它。
- 我尝试了一些 groovy 脚本,但没有得到任何结果。
**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) `
【问题讨论】: