我想使用pipe
将可读流传递给帖子请求正文,但我遇到了麻烦。这是我的代码:
var request = require('request');
var fs = require('fs');
var source = fs.createReadStream('./originalJsonDataWithObject.json'); //creating a read stream to read the file
source.pipe(request.post('http://localhost:3030/decompress')); //piping it to the post request
答案 0 :(得分:0)
var request = require('request');
var fs = require('fs');
var file = fs.createReadStream('./originalJsonDataWithObject.json');
var req = request.post({
url: 'your post url',
headers: {<headers>},
body: file
});
POST请求正文参数是您实际发送的数据 请求。这些数据可以有多种形式(流,缓冲,字符串等) 你不需要管道。如果您需要发布JSON数据,可以执行以下操作:
const req = request.post({
url: 'http://localhost:3030/decompress',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(<your JSON data>)
});
答案 1 :(得分:-1)
var request = require('request');
var fs = require('fs');
var file = fs.createReadStream('./originalJsonDataWithObject.json');// This line opens the file as a readable stream.
http.get(url, function (response) {
response.pipe(file);// this line just create a stream and move to file into stream
});