有一个GET API REST调用请求,在执行时给出一个zip文件,这里是响应的标题
content-disposition →attachment;filename="results.zip"
content-type →text/plain; charset=UTF-8
在Postman上,我们可以Send and Download
并保存生成的zip文件
我使用RestAssured
来测试REST API调用。
有人可以帮我解决如何从API调用中检索生成的zip文件吗?
答案 0 :(得分:0)
更新: 我找到了Rest Assured的解决方案,我们可以将Response作为InputStream或Byte Array获取,并进一步写入ZipFile。
作为InputStream: -
// Get the response as Input Stream
InputStream is = result.getBody().asInputStream();
OutputStream stream = new FileOutputStream("D:\\Test.zip");
int read = 0;
byte[] bytes = new byte[1024];
while ((read = is.read(bytes)) != -1) {
stream.write(bytes, 0, read);
}
System.out.println("Zip file captured successfully");
As ByteArray: -
//以ByteArray
获取响应byte[] arraybyteResponse = result.getBody().asByteArray();
ByteBuffer buffer = ByteBuffer.wrap(arraybyteResponse);
OutputStream os = new FileOutputStream("D:\\Test.zip");
WritableByteChannel channel = Channels.newChannel(os);
channel.write(buffer);
System.out.println("Zip file captured successfully");