[英]Correct way to stream a `request` request?
I am trying to create a ReadableStream object from a request
, which eventually I want to pass it into the attachment variable for mailgun-js 's data object: 我试图从一个
request
创建一个ReadableStream对象,最终我想将其传递给mailgun-js数据对象的附件变量:
var fileStream = null;
request(String(url)).pipe(fileStream);
msg.attachment = new mailgun.Attachment({
data: fileStream,
filename: 'my_custom_name.png',
knownLength: fileStat.size,
contentType: 'image/png'});
What's the correct way to do this? 正确的方法是什么?
I know this is a little late, but hopefully still helpful. 我知道这有点晚了,但希望对您有所帮助。 Just submitted a pull request to have this functionality:
刚提交拉取请求以具有此功能:
var request = require('request');
var file = request("https://www.google.ca/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png");
var data = {
from: 'Excited User <me@samples.mailgun.org>',
to: 'serobnic@mail.ru',
subject: 'Hello',
text: 'Testing some Mailgun awesomness!',
attachment: file
};
mailgun.messages().send(data, function (error, body) {
console.log(body);
});
If it doesn't get merged, you can see it here: https://github.com/antoniosou/mailgun-js 如果没有合并,则可以在这里查看: https : //github.com/antoniosou/mailgun-js
Not tested, but worth giving a try: 未经测试,但值得尝试:
var stream = new require('stream').PassThrough();
request(String(url)).pipe(stream);
msg.attachment = new mailgun.Attachment({
data : stream,
filename : 'my_custom_name.png',
knownLength : fileStat.size,
contentType : 'image/png'
});
request()
doesn't seem to inherit from Stream
, so directly passing it as data
property won't work. request()
似乎并不继承自Stream
,因此将它作为data
属性直接传递将不起作用。 Instead, a PassThrough
stream is created, which inherits from both Readable
and Writable
. 而是创建了一个
PassThrough
流,该流继承自Readable
和Writable
。
The Readable
part will be used by the Mailgun class, and the Writable
part will get the HTTP response data piped into it. Mailgun类将使用
Readable
部分,而Writable
部分将获得HTTP响应数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.