【问题标题】:How to retrieve the Json response having attachment如何检索具有附件的 Json 响应
【发布时间】:2017-10-31 09:55:08
【问题描述】:

有一个 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 文件吗?

【问题讨论】:

    标签: json rest-assured


    【解决方案1】:

    更新: 我从 Rest Assured 那里找到了一个解决方案,我们可以将 Response 作为 InputStream 或 Byte Array 获取并进一步写入 ZipFile。

    作为输入流:-

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

    作为字节数组:-

    // 以字节数组的形式获取响应

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

    【讨论】:

      猜你喜欢
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-15
      • 2011-04-22
      • 1970-01-01
      • 1970-01-01
      • 2019-01-12
      相关资源
      最近更新 更多