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

POST body is not forwarded in the proxied request #741

Closed
2 tasks done
giuseppepastore10 opened this issue Apr 7, 2022 · 2 comments
Closed
2 tasks done

POST body is not forwarded in the proxied request #741

giuseppepastore10 opened this issue Apr 7, 2022 · 2 comments

Comments

@giuseppepastore10
Copy link

giuseppepastore10 commented Apr 7, 2022

Checks

Describe the bug (be clear and concise)

Hi,
I'm developing a React application and I configured my setupProxy.js but it seems no body data is forwarded to the server in the proxied request. What could be the problem?

Step-by-step reproduction instructions

...

Expected behavior (be clear and concise)

Forward the request with the body to the target host

How is http-proxy-middleware used in your project?

[email protected] /*****/*****/*****/*****/*****/gymnasio-management-app
├── [email protected]
└─┬ [email protected]
  └─┬ [email protected]
    └── [email protected] deduped

What http-proxy-middleware configuration are you using?

const {createProxyMiddleware, fixRequestBody} = require('http-proxy-middleware');
const bodyParser = require('body-parser')

module.exports = function(app) {
 var restream = function(proxyReq, req, res, options) {
   if (req.body) {
       let bodyData = JSON.stringify(req.body);
       proxyReq.setHeader('Content-Type','application/json');
       proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
       proxyReq.write(bodyData)
       proxyReq.end();
   }
 }
 
 app.use(bodyParser.json());
 app.use(bodyParser.urlencoded());
 app.use(
   '/api/*',
   createProxyMiddleware({
     target: 'https://<domain>/',
     changeOrigin: true,
     pathRewrite: {
       '^/api/': '<path>'
     },
     logLevel:'debug',
     onProxyReq: restream,  
   })
 );  

};

What OS/version and node/version are you seeing the problem?

macOS Big Sur v11.5.2
Node v16.13.2

Additional context (optional)

No response

@giuseppepastore10 giuseppepastore10 changed the title Body is not forwarded in the proxied request POST body is not forwarded in the proxied request Apr 7, 2022
@chimurai
Copy link
Owner

chimurai commented Apr 9, 2022

Did you try the fixRequestBody instead of your own restream?

const { createProxyMiddleware, fixRequestBody } = require('http-proxy-middleware');

const proxy = createProxyMiddleware({
  /**
   * Fix bodyParser
   **/
  onProxyReq: fixRequestBody,
});

https://github.com/chimurai/http-proxy-middleware#intercept-and-manipulate-requests

@chimurai
Copy link
Owner

dupe #40

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

No branches or pull requests

2 participants