简体   繁体   English

后端从第三方获取 pdf。 我如何通过 REST api 发送给我的客户?

[英]Backend gets a pdf from a third-party. How can I send it via REST api to my client?

I get a pdf from a third party.我从第三方那里得到一个 pdf。 I save the file on S3.我将文件保存在 S3 上。 I can see the file on S3 and when I open it I can see my PDF. However, when I passed the pdf to the client and check it on Postman I get an empty PDF.我可以在 S3 上看到该文件,当我打开它时,我可以看到我的 PDF。但是,当我将 pdf 传递给客户端并在 Postman 上检查它时,我得到一个空的 PDF。

Here is my code:这是我的代码:

public async getReportFromThirdParty(token) {
    const params = {
        headers: { Authorization: `Bearer ${token}`},
        responseType: "arraybuffer",
    }  

    let report = {};
    report = await axios.get(`https://api.thirdparty.com/api/get-pdf`, params);
    return report.data;
}


app.post("/download", async (req, res) => {
   const token = 'abcde-secret-token';
   const pdf = await getReportFromThirdParty(token);
   await saveToS3(pdf) // <---- I checked and it saves the file properly on S3 as PDF
 
   res.contentType("application/pdf");
   return res.status(200).send(pdf); // <--- this returns an empty pdf file
});

Any ideas?有任何想法吗?

im going to guess your app is an node express app so therefore you likely need to我猜你的应用程序是一个node express app ,因此你可能需要

res.sendFile();

More info found on the express site快递网站上找到更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查我的公共API的XMLHttpRequest是来自我自己的webapp还是来自第三方客户端(以确保优先级)? - How can I check if an XMLHttpRequest to my public API is from my own webapp or from a third-party client (to ensure priority)? 如何从第三方 REST API 获取数据并显示在页面上 - How to fetch the data from third party REST API and and display on page 如何从第三方 REST API 获取数据并显示在页面上 - How to fetch the data from third party REST API and and display on the page 如果向第三方 API 发出错误请求,Node JS Express 如何发送 404 错误? - Node JS Express how can I send a 404 error if a bad request is made to third party API? 如何使用代码破坏网站上的第三方iframe? - How can I break a third party iframe on my site, with code? 如何从我自己的库中的第三方 JavaScript 库中导出所有内容? - How can I export everything from a third party JavaScript library in my own library? 如何将我从第三方 API 获取的数据存储在我的快速服务器中,并在以后安排另一个 API 调用? - How to store the data I've fetched from a third-party API in my express server and schedule another API call at a later time? 在客户端上,如何下载映像,然后以POST请求的形式发送到第三方服务器? - On client, how do I download an image, then send as POST request to third party server? 如何包装来自第三方api的错误? - How to wrap errors from third party api? 如果javascript在第三方站点上,如何从javascript进行rest api的身份验证? - how to do authentication of rest api from javascript, if javascript is on third party site?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM