【问题标题】:How to download excel (.xls) file from API in postman?如何从邮递员的 API 下载 excel (.xls) 文件?
【发布时间】:2016-12-22 21:02:50
【问题描述】:

我有一个 API 端点和该 API 的授权令牌。

上述 API 用于.xls 报告下载,我如何使用(如果可能)Postman 查看下载的.xls 文件?

如果无法使用 Postman 我应该寻找哪些其他编程方式?

【问题讨论】:

  • @nbokmans 我想在使用邮递员时下载后端提供的 .xls 文件,但我无法正确查看 .xls 文件,它的所有 unicode 和特殊字符。我需要的是,如果除了邮递员之外还有其他编程方式可以触发 api 并在我的电脑上下载 .xls 文件
  • @nbokmans 感谢回复,刚开始下载,没有给出任何位置。

标签: rest postman rest-client advanced-rest-client


【解决方案1】:

发出请求时尝试选择send and download 而不是send。 (蓝色按钮)

https://www.getpostman.com/docs/responses

“对于二进制响应类型,您应该选择Send and download,它可以让您将响应保存到您的硬盘。然后您可以使用适当的查看器查看它。”

【讨论】:

  • 这绝对不直观 - 感谢您的指点!试过 Save Response 但它只是保存到 Examples 这没有帮助。
【解决方案2】:

您可以通过 postman 中响应右侧的选项来保存响应(pdf、doc 等) 检查这张图片

更多详情请看这里

https://learning.getpostman.com/docs/postman/sending_api_requests/responses/

【讨论】:

  • 好像没问题。但是 GET 或 POST 请求没有保存响应选项。我应该在 Postman 上使用 发送和下载 选项吗?
【解决方案3】:

在邮递员中 - 您是否尝试将标题元素“接受”添加为“应用程序/vnd.ms-excel”

【讨论】:

    【解决方案4】:

    如果端点确实是 .xls 文件的直接链接,您可以尝试以下代码来处理下载:

    public static boolean download(final File output, final String source) {
        try {
            if (!output.createNewFile()) {
                throw new RuntimeException("Could not create new file!");
            }
            URL url = new URL(source);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // Comment in the code in the following line in case the endpoint redirects instead of it being a direct link
            // connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("AUTH-KEY-PROPERTY-NAME", "yourAuthKey");
            final ReadableByteChannel rbc = Channels.newChannel(connection.getInputStream());
            final FileOutputStream fos = new FileOutputStream(output);
            fos.getChannel().transferFrom(rbc, 0, 1 << 24);
            fos.close();
            return true;
        } catch (final Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    

    应该需要做的就是为授权令牌设置正确的名称并填写它。

    示例用法:

    download(new File("C:\\output.xls"), "http://www.website.com/endpoint");
    

    【讨论】:

    • 这段代码对我有用,但如果我的端点不是直接链接,我需要在上面的代码 sn-p 中做哪些更改?
    猜你喜欢
    • 1970-01-01
    • 2019-11-12
    • 2021-09-22
    • 2016-06-10
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    相关资源
    最近更新 更多