简体   繁体   English

如何传递POST请求

[英]How to pipe POST requests

I'm a bit confused about how to pipe some data. 我对如何通过管道传输一些数据感到困惑。

I've got some pipes working and chained such that I not have an output stream containing the data I want to input to a request POST 我有一些管道在工作并且已链接起来,这样我就没有包含要输入到请求POST的数据的输出流。

var options = {
    host: 'localhost',
    port: 8529,
    path: '/_api/cursor',
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': data.length
    }
}

var req = http.request(options);

I would normally just action 'mystreams2.pipe(req)' but how do I set the 'data.length' value ? 我通常只需要操作'mystreams2.pipe(req)',但是如何设置'data.length'值呢?

(I'm using the streams2 interface not the old stream format) (我使用的是streams2接口,而不是旧的流格式)

By assumption that you don't have a massive amount of data in your buffer, you first need to collect the data in order to find its length. 假设缓冲区中没有大量数据,则首先需要收集数据以查找其长度。

var source = /* a readable stream */;
var data = '';

source.on('data', function(chunk) {
  data += chunk;
});
source.on('end', function() {
  var options = {
    host: 'localhost',
    port: 8529,
    path: '/_api/cursor',
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Content-Length': data.length
    }
  };

  var req = http.request(options);
  req.end(data);
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM