PassThrough scenarios do not work for requests with JSON body
kgorbiel opened this issue · comments
PassThrough scenarios do not work for requests with JSON body (JSON body of request and not response).
In this sitution the request is forwarded to the server, but reading JSON content result in error:
java.io.EOFException: Connection closed: Read failed. Possible end of stream encountered.
Above is JAVA application server (Websphere Liberty), but the same problem has been reproduced with Postman mock server:
C:\Ontwikkel\WAS9\VerhuisDialoogFrontend\node_modules\http-proxy\lib\http-proxy\index.js:120
throw err;
^
Error: read ECONNRESET
at TLSWrap.onStreamRead (internal/stream_base_commons.js:209:20)
{ code: 'ECONNRESET', syscall: 'read' }
Exactly the same request but with the body content type changed to e.g. text will arrive at the server.
So it looks it is something related to JSON parser.
I have this problem with with POST requests but the same seems to happen also when I send GET with JSON body.
Is this maybe related ?:
https://github.com/expressjs/body-parser/issues/207
The problem is now fixed.
It was related to the http-proxy.
JSON body was already parsed by the body parser. The stream was not usable any more.
In the http-poxy, we make a new stream now:
apiProxy.on('proxyReq', function (proxyReq, req, res, options) {
if (!req.body || !Object.keys(req.body).length) {
return;
}
if (proxyReq.getHeader('Content-Type').includes('application/json')) {
var bodyData = JSON.stringify(req.body);
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
proxyReq.write(bodyData);
}
});