You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Postman调用接口获Base64文档:如何解码并本地下载?

在Postman中解码Base64编码的文档并下载到本地

方法一:用Postman Tests脚本自动处理

  1. 调用接口https://testURL.com/getDocument,确认请求的Content-Type已设为application/xml。
  2. 切换到Tests标签页,粘贴以下脚本:
// 若响应是XML格式,先解析提取docContent的Base64内容
const xmlDoc = new DOMParser().parseFromString(pm.response.text(), "application/xml");
const base64Content = xmlDoc.querySelector("docContent").textContent;

// 解码Base64并转换为Blob
const decodedBytes = atob(base64Content);
const byteNumbers = Array.from(decodedBytes).map(c => c.charCodeAt(0));
const byteArray = new Uint8Array(byteNumbers);
// 根据实际文档类型调整MIME类型,比如docx用application/vnd.openxmlformats-officedocument.wordprocessingml.document
const blob = new Blob([byteArray], { type: "application/octet-stream" });

// 触发浏览器下载
const url = window.URL.createObjectURL(blob);
const downloadLink = document.createElement("a");
downloadLink.href = url;
downloadLink.download = "target-document.doc"; // 替换为目标文件名及后缀
document.body.appendChild(downloadLink);
downloadLink.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(downloadLink);
  1. 点击Send发送请求,脚本会自动完成解码并触发文档下载。

方法二:手动复制解码保存

  1. 发送请求后,在Response标签页复制返回的Base64编码字符串(即JVBTEKJhekjdjhjkegje....这类内容)。
  2. 打开Postman控制台(右上角控制台图标),输入以下代码(替换Base64字符串)并回车:
const targetBase64 = "这里替换成你复制的Base64内容";
const decodedStr = atob(targetBase64);
const byteArray = new Uint8Array(Array.from(decodedStr).map(c => c.charCodeAt(0)));
const blob = new Blob([byteArray], { type: "application/octet-stream" });

const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "target-document.doc";
a.click();
URL.revokeObjectURL(url);
  1. 浏览器会自动下载解码后的文档。

注意事项

  • 请根据实际文档类型调整脚本中的type参数和文件名后缀,比如PDF用application/pdf,Excel用application/vnd.openxmlformats-officedocument.spreadsheetml.sheet。
  • 若响应结构特殊,需调整XML解析逻辑确保准确提取docContent的纯Base64内容。

内容的提问来源于stack exchange,提问作者Naresh Naidu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 08:47:06