我正在尝试从包含 pdf 的远程 URL 发出 Get 请求,并从其响应中获取包含原始数据的缓冲区,并将其用作 sendGrid 的附件,但这似乎不起作用,因为发送的 pdf 是空的。
下面找到我使用的代码片段:
Parse.Cloud.httpRequest({ url:"http://<MY_URL>/x.pdf",
headers:{
"X-Parse-Application-Id":"<MY_ID>",
"X-Parse-REST-API-Key": "<MY_KEY>",
}
}).then(function(httpResponse) {
//email.addFileFromBuffer('x.pdf', httpResponse.buffer);
//-- Doesn't work raise and exception
email.addFileFromBuffer(request.params.toName+'_report.pdf', new Buffer(httpResponse.text));
sendgrid.send(email).then(function() {
response.success("Email sent!");
}, function(e) {
console.log(e);
response.error("Uh oh, something went wrong with sending email");
});
}, function(e) {
console.log(e);
response.error("Uh oh, something went wrong with pdf");
});
如果有人对此有不同的想法。
非常感谢!
编辑1:
使用时:
email.addFileFromBuffer(request.params.toName+'_report.pdf', httpResponse.buffer);
结果:
Ran cloud function sendEmail with:
Input: {"fileName":"x.pdf","toEmail":"xxx@gmail.com","toName":"XXX"}
Result: URIError: URI malformed
at encodeURIComponent (native)
at <anonymous>:325:38
at Parse.js:1:21050
at Array.forEach (native)
at Function.x.each.x.forEach (Parse.js:1:661)
at b._objectEach.b._each (Parse.js:1:21023)
at formEncode (<anonymous>:321:5)
at Object.Parse.Cloud.httpRequest (<anonymous>:532:24)
at SendGrid.send (sendgrid.js:292:19)
at main.js:29:12
最佳答案
Parse.Cloud.httpRequest
返回一个具有原始字节和字符串表示形式的 response
对象。您不需要创建新的缓冲区,只需使用 response.buffer
。
挖掘它:
email.addFileFromBuffer(request.params.toName+'_report.pdf', httpResponse.buffer);
关于javascript - 从 httpResponse 获取包含 pdf 的缓冲区并将其用作附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30041175/