javascript - curl 可以获取 PDF,但 JavaScript 获取不能
问题描述
我有一个特定的 API 调用来获取 PDF,它与 curl 完美配合:
curl --location --request GET "https://app.hiddensite.com/api/v2/dn/jobs/export/INV-00181?format=pdf&document=pod&type=Delivery" --header "x-api-key: cfe5750a5d5114xxxxxxxxxxxx49dabb3e98c0cc63424cb014f" > a.pdf
但是,当我这样打电话时:
const r = await fetch('https://app.hiddensite.com/api/v2/dn/jobs/export/INV-00181?format=pdf&document=pod&type=Delivery',
{
method: 'GET',
headers: {
'x-api-key': 'cfe5750a5d5114xxxxxxxxxxxx49dabb3e98c0cc63424cb014f',
},
});
return r.text()
我得到一个大小略有不同的 PDF,几乎可以正确显示,但图形消失了,只有文本 - 即它在某种程度上有点畸形。无论是尝试 fs.writeFileSync() 还是只是 console.logging 输出和管道到文件,我都会得到这个。
知道是什么导致这些略有不同吗?
解决方案
PDF 并不完全是文本文件,因此尝试将其作为一个文件来阅读会在.text()
其二进制部分(例如图形!)中引入错误。
你需要r.blob()
.
推荐阅读
- oracle-apex-5.1 - Oracle APEX - 有没有办法从列表条目中调用 javaScript
- angular - 尝试编辑数据表行时无法为 p-autocomplete 设置 FormGroup 值
- node.js - How to use 'Markdown' in parse_mode of telegram bot?
- javascript - 如何处理“main.js:11 Uncaught TypeError: Cannot read property 'addEventListener' of null”这样的错误?
- algorithm - 将“n”划分为三个平方和的数量(快速算法)
- java - 如何创建泛型类数组?(不是泛型数组!)
- javascript - 在我放入一个无法自行运行的新函数后,整个 JS 文件无法运行
- javascript - 如何获取循环值
- java - 如何使用 feign 客户端反序列化 application/json+hal 消息?
- file - UNIX Shell:else 部分中的代码不会执行,但会检查条件