我正在尝试从包含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
答案 0 :(得分:0)
Parse.Cloud.httpRequest
返回一个具有原始字节和字符串表示的response
对象。您无需创建新缓冲区,只需使用response.buffer
。
挖掘它:
email.addFileFromBuffer(request.params.toName+'_report.pdf', httpResponse.buffer);
答案 1 :(得分:-1)
以下是如何获得缓冲区:
Parse.Cloud.httpRequest('http://domain.net/assets/file.zip')
.then(function(httpResponse) {
httpResponse.buffer // <---- file contents here
});
您不能在此模块中使用来自二进制文件的缓冲区(它应该使用普通/文本)。请改为https://github.com/m1gu3l/parse-sendgrid-mailer。