http request does not emit 'end' after proxy

356 views Asked by At

I need to be able to use the http request body in my request proxy application and then again in the actual web service. I am using restreamer to 'reset' the stream (and even wrote the middleware myself with no change). The web service receives the body just fine, but because end is never emitted, I cannot continue with the request.

Testing with postman, sending a raw body, with content type set. Any suggestions would be greatly appreciated. Thanks!

var   express = require('express')
    , bodyParser = require('body-parser')
    , http = require('http')
    , restreamer = require('connect-restreamer')
    , httpProxy = require('http-proxy')

var app = express();

app.use(function (req, res, next) {
    var body = '';
    req.on('data', function (chunk) {
        body += chunk.toString('utf8');

    });
    req.on('end', function (chunk) {
        req.body = JSON.parse(body)
        next();
    });
});

app.use(restreamer());

var proxy = httpProxy.createServer();

app.all('*', function (req, res) {
    proxy.web(req, res, {
        target: 'http://localhost:8001'
    });
});

http.createServer(app).listen(8000);

app2 = express();

app2.use(function (req, res, next) {
    var body = '';
    req.on('data', function (chunk) {
        body += chunk.toString('utf8');

    });
    req.on('end', function (chunk) {
        req.body = JSON.parse(body)
        next();
    });
});

app2.all('*', function (req, res) {
    res.send(req.body)
});

http.createServer(app2).listen(8001);
1

There are 1 answers

0
ngourley On BEST ANSWER

Using the request library in my application, it worked:

var request = require('request')
request.post({
    url: 'http://localhost:8000',
    json: {content: 123, type: "greeting", access_token: "here i am"}
},function(err, res,data){
    console.log('return:' ,err, data)
});

But using curl with a file containing the same message, it would not work:

curl -X POST -d @data.json http://localhost:8000 --header "Content-Type:application/json"

I compared the request objects against each other and found a few differences and when I got to the request header for content-length, I found that editing it the "correct" length would end the steam (and the web server would send a response).

I will make the modifications needed and commit to connect-restreamer module.