Skip to content

POST fails/hangs examples to restream also not working #1279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
isawk opened this issue Jul 9, 2018 · 5 comments
Open

POST fails/hangs examples to restream also not working #1279

isawk opened this issue Jul 9, 2018 · 5 comments

Comments

@isawk
Copy link

isawk commented Jul 9, 2018

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

@connelhooley
Copy link

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.

@loretoparisi
Copy link

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

@isawk
Copy link
Author

isawk commented Oct 3, 2018 via email

@loretoparisi
Copy link

@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
                });

@taozhi8833998
Copy link

same here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants