简体   繁体   English

传输“请求”请求的正确方法?

[英]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流,该流继承自ReadableWritable

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.

相关问题 Angular:这是构造$ http POST请求的正确方法吗? - Angular: Is this the correct way to structure a $http POST request? 设置POST请求的内容语言的正确方法 - Correct way to set the content language of a POST request 从请求分派Redux动作的正确方法 - Correct way to dispatch redux action from request 使用jQuery添加请求标头的正确方法 - Correct way to add request header with jQuery 在SPRING MVC中使用JSON发出POST请求的正确方法? - Correct way of making POST request with JSON in SPRING MVC? Django Channels - 拒绝未经授权的 websocket 请求的正确方法? - Django Channels - correct way to reject an unauthorized websocket request? 从Express / connect中间件“结束”请求的正确方法是什么? - What is the correct way to “end” a request from express/connect middleware? 使用 axios 和异步等待循环请求的正确方法是什么? - What is the correct to way to loop request with axios and async await? 在 React 中成功发布请求后重定向的正确方法是什么? - What is the correct way of redirecting after successful post request in React? 在 React 功能组件中取消异步 axios 请求的正确方法 - Correct way to cancel async axios request in a React functional component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM