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

How to modify req.body? #251

Open
Doun72 opened this issue Apr 28, 2020 · 3 comments
Open

How to modify req.body? #251

Doun72 opened this issue Apr 28, 2020 · 3 comments

Comments

@Doun72
Copy link

Doun72 commented Apr 28, 2020

I try to modify the parameters (password) from proxyOnRequest before sending them the web server.
In my index.js, I have the following "redbird" function:
reverseProxy.register("node:5470", "web:4000", {ssl:false, useTargetHostHeader: true});

Then I call this function:
reverseProxy.proxy.on('proxyReq', changetohash.onRequest());

Inside the "changetohash" function, I want to modify the received password and send it again as parameter for the web server.

The only way to retrieve the parameter is to use this function (request.on):

export function onRequest (options) {
return function hashOnRequest(proxyRequest, request, response, options) {
if (((proxyRequest.path == "/signup") || (proxyRequest.path == "/login")) && (request.method == 'POST')) {
// Make any needed POST parameter changes
let body = '';
request.on('data', (chunk) => {
body += chunk;
}).on('end', () => {
//I try to modify the request header here
});
....

but when I wanted to modify the request header:
request.setHeader( 'content-length', body.length );

I obtain a error: "the request is already closed"
The 'content-type' is 'application/x-www-form-urlencoded'

Any ideas how to retrieve the body of my request? I tried directly request.body but it didn't work.

Thanks for your help.

@amalhotragannett
Copy link

we need to wait till the request has been finished.

function waitTillRequestEnds(request) {
 // you can change the logic to what you want on the request fields method, path, ....
  if (request.method === 'POST') {
    return new Promise((resolve) => {
      const body = [];
      request.on('data', (chunk) => {
        body.push(chunk);
      });
      request.on('end', () => {
        resolve(Buffer.concat(body).toString());
      });
    });
  } else {
    return;
  }
};

Now we will listen to the event proxyReq on the proxy server.

proxy.on('proxyReq', async (proxyReq, request, res, options) => {
  const requestBody = await waitTillRequestEnds(request); // will return the request body
  const newBody = requestBody.concat(' ', 'bar'); // changing the body
  proxyReq.setHeader('x-header', 'my-custom-header'); // changing the header
  proxyReq.write(newBody); // restream body before proxying 
});

@Doun72
Copy link
Author

Doun72 commented May 8, 2020

Thanks for your help. I tried what you proposed but I had the following error message on this line(proxyRequest.setHeader('x-header','my-custom-header');):
Cannot set headers after they are sent to the client
at ClientRequest.setHeader (_http_outgoing.js:541:11)
at ProxyServer.hashOnRequest

@amalhotragannett
Copy link

Are you behind a corporate firewall? There is an open issue with node-http-proxy which is the backend for redbird.

There is a workaround mentioned on the issue. Check if that helps you resolve the issue.

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

2 participants