Skip to content
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

Cannot proxy after parsing body #299

Closed
connelhooley opened this issue Sep 22, 2018 · 10 comments
Closed

Cannot proxy after parsing body #299

connelhooley opened this issue Sep 22, 2018 · 10 comments

Comments

@connelhooley
Copy link

connelhooley commented Sep 22, 2018

I cannot re-send a proxied request's body after I have parsed it.

Using this recipe, this comment and this comment I have written the following example:

import express from 'express';
import proxy from 'http-proxy-middleware';
import bodyParser from 'body-parser'

const app = express();
app.use(bodyParser.json());
app.post('*', proxy({
    target: 'http://target.here.com',
    secure: false,
    onProxyReq: (proxyReq, req) => {
        if (req.body) {
            const bodyData = JSON.stringify(req.body);
            proxyReq.setHeader('Content-Type','application/json');
            proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
            proxyReq.write(bodyData);
        }
    }
}));
app.listen(2001, () => console.log('Listening'));

I get an error when trying to set the first header on the proxyReq saying: Cannot set headers after they are sent to the client.

If that is the case how does the recipe work?

@connelhooley
Copy link
Author

Looks related to:
http-party/node-http-proxy#1279

@SBRK
Copy link

SBRK commented Sep 26, 2018

+1 got the same issue. Moving the middlewares before body.json works

@connelhooley
Copy link
Author

+1 got the same issue. Moving the middlewares before body.json works

Yes that's correct. I need to parse the body to know if I need to proxy the request though.

@windix
Copy link

windix commented Dec 13, 2018

If need to parse the body before proxy, I have followed suggestion from this issue: #177 (comment)

@taozhi8833998
Copy link

same issue

@ThalesMatoso
Copy link

+1

1 similar comment
@risu-p
Copy link

risu-p commented Dec 12, 2019

+1

@stuartZhang
Copy link

This is my solution: https://github.com/stuartZhang/coexist-parser-proxy

@chimurai
Copy link
Owner

fixed in #492

@phoenixstudiodz
Copy link

The solutions proposed here worked fine, but after adding an agent (sock5 SocksProxyAgent from socks-proxy-agent) I started getting Can't set headers after they are sent to the client.
I fixed that by overriding verify in the bodyParser.

//
const bodyParser = require('body-parser') 
const { Readable } = require('stream');
//
app.use(bodyParser.json({
  // verify function has access to buff that contains the stream
  // create a new readable stream and save it to streamBody
  verify: (req, res, buf, encoding) => {
  const readableStream = Readable.from(buf);
  req.streamBody = readableStream
 }
}));
 // Param "buffer" accept a stream if the original one was consumed
app.all("*", (req, res) => proxy.web(req, res, {
 target: "target-here",
 buffer: req.streamBody
}); 

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

9 participants