如何从API在邮差下载excel (.xls)文件?

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

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

如果不能使用邮递员,我应该寻找的其他编程方式是什么?

236607 次浏览

如果端点确实是到.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");

在postman -你是否尝试过将头元素“Accept”添加为“application/vnd.ms-excel”

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

截图显示 . quot

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

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

你可以只保存响应(pdf,doc等.)在邮差的响应右侧的选项 检查这张图片 邮递员保存响应 < / p >

欲了解更多详细信息,请查看此

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

对于UI,在Postman中选择发送和下载按钮而不是发送按钮: enter image description here < / p >