简体   繁体   中英

Node's request does not pass POST requests using pipe

Proxy written using node/express and request fails to pipe POST calls, GET seems to work:

var pipeToTrustedZone = function(req, res){ 
    getEnrichedHeaders(req,function(err, headers){
    req.headers = headers;
    var url = proxiedServerPath + req.originalUrl;
    console.log(req.method+" TOWARDS");
    log.info(url);
    req.pipe(request({qs:req.query, uri: url })).pipe(res);
    });
}

The above code executes get requests just fine placed as a middleware in an express router, however on POST the proxiedServer never receives the message.

Any idea why the above does not work?

Also my app is using body parser middlewares since not all endpoints are to be proxied:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

You have to both set request headers in the object passed to request() and set the appropriate headers on res . That means you won't be able to simply make it one-liner. For example:

req.pipe(request({
  qs: req.query,
  uri: url,
  headers: req.headers
})).on('response', function(pres) {
  res.writeHead(pres.statusCode, pres.headers);
  pres.pipe(res);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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