Node-http-proxy: POST fails/hangs examples to restream also not working

Created on 9 Jul 2018  Β·  5Comments  Β·  Source: http-party/node-http-proxy

example (https://github.com/nodejitsu/node-http-proxy/blob/master/examples/middleware/bodyDecoder-middleware.js) to restream body for 1.17.0 not working in order to send data with post method

Node: v8.11.2
node-http-proxy: 1.17.0

Most helpful comment

@isawk thanks a lot! This is the only way it worked πŸ‘ πŸ’― πŸ”’ πŸ₯‡

               proxy.once('proxyReq', function (proxyReq, req, res, options) { //restream
                    let bodyData;
                    if (!req.body || !Object.keys(req.body).length) {
                        return;
                    }
                    switch( proxyReq.getHeader('Content-Type') ){
                        case "application/json":
                        case "application/json; charset=UTF-8": 
                            bodyData = JSON.stringify(req.body);
                        break;
                    case "application/x-www-form-urlencoded":
                        bodyData = querystring.stringify(req.body);
                        break;
                    default: 
                    }
                    if (bodyData) {
                        console.debug("---[proxyReq]----", bodyData);
                        proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
                        proxyReq.write(bodyData);
                        proxyReq.end();
                    }
                });
                proxy.on('proxyRes', function (proxyRes, req, res) {
                    modifyResponse(res, proxyRes, function (response) {
                        console.log("----[proxyRes]---->", response)
                        // modify response eventually
                        return response; // return value can be a promise
                    });
                });
                proxy.web(req, res, {
                    //  url string to be parsed with the url module
                    target: target_uri,
                    //  true/false, Default: false - changes the origin of the host header to the target URL
                    changeOrigin: true,
                    //  true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request 
                    ignorePath: true
                });

All 5 comments

Are there any updates on this? Would downgrading to an earlier version help? If so does anyone know how to find out what version the example was written against?

I'm getting a Cannot set headers after they are sent to the client error.

@isawk any chance to get this fixed, I'm having the same issue, see above links. Thanks.

I resolved issue with the following code:

Am using nestjs however this should work regardless of your implementation.

take note of this.apiProxyServer.once('proxyReq', appears proxy runs
multiple times, i found this once function whiles inspecting code base

@All("/end-points")
proxy(@Req() request, @Res() response) {

this.apiProxyServer.once('proxyReq', function (proxyReq, req, res, options)
{

console.debug("Proxying request");

let bodyData;

if (!req.body || !Object.keys(req.body).length) {
return;
}

switch( proxyReq.getHeader('Content-Type') ){
case "application/json":
case "application/json; charset=UTF-8":bodyData = JSON.stringify(req.body);
break;
case "application/x-www-form-urlencoded":bodyData = querystring.stringify(
req.body);break;
default: console.debug("unable to process content type ", proxyReq.getHeader
('Content-Type') );
}

if (bodyData) {
console.debug("request has body data, proxying");
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
proxyReq.write(bodyData);
}
});
this.apiProxyServer.web(request, response);
}
}

On Tue, Sep 25, 2018 at 6:31 PM Loreto Parisi notifications@github.com
wrote:

@isawk https://github.com/isawk any chance to get this fixed, I'm
having the same issue, see above links. Thanks.

β€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/nodejitsu/node-http-proxy/issues/1279#issuecomment-424409978,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AUDbqSCEFJwQXKaXyxD3vg6wRtbFHt90ks5uelppgaJpZM4VILNo
.

--
Kwasi Gyasi-Agyei
I am part of an EDGED crowd

Mobile +27 (81) 466 4488
Email [email protected]
Skype kwasi.gyasiagyei
Web http://edged.agency

@isawk thanks a lot! This is the only way it worked πŸ‘ πŸ’― πŸ”’ πŸ₯‡

               proxy.once('proxyReq', function (proxyReq, req, res, options) { //restream
                    let bodyData;
                    if (!req.body || !Object.keys(req.body).length) {
                        return;
                    }
                    switch( proxyReq.getHeader('Content-Type') ){
                        case "application/json":
                        case "application/json; charset=UTF-8": 
                            bodyData = JSON.stringify(req.body);
                        break;
                    case "application/x-www-form-urlencoded":
                        bodyData = querystring.stringify(req.body);
                        break;
                    default: 
                    }
                    if (bodyData) {
                        console.debug("---[proxyReq]----", bodyData);
                        proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
                        proxyReq.write(bodyData);
                        proxyReq.end();
                    }
                });
                proxy.on('proxyRes', function (proxyRes, req, res) {
                    modifyResponse(res, proxyRes, function (response) {
                        console.log("----[proxyRes]---->", response)
                        // modify response eventually
                        return response; // return value can be a promise
                    });
                });
                proxy.web(req, res, {
                    //  url string to be parsed with the url module
                    target: target_uri,
                    //  true/false, Default: false - changes the origin of the host header to the target URL
                    changeOrigin: true,
                    //  true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request 
                    ignorePath: true
                });

same here

Was this page helpful?
0 / 5 - 0 ratings

Related issues

miton18 picture miton18  Β·  5Comments

jsmylnycky picture jsmylnycky  Β·  3Comments

xingxinglail picture xingxinglail  Β·  6Comments

SET001 picture SET001  Β·  5Comments

robertjchristian picture robertjchristian  Β·  6Comments